If
I want to find files contains a string i created
means you want to check file's content (not name) You have to load the file, e.g. (assuming stringToFind
doesn't have line breaks)
string[] dirs = Directory
.EnumerateFiles(@"c:\", "*.txt"); // all txt files (put the right wildcard)
.Where(file => File
.ReadLines(file) // with at least one line
.Any(line => line.Contains(stringToFind))) // which contains stringToFind
.ToArray();
Edit: In case you want files' names which contain c
, e.g. "mycode.txt"
, "Constraints.dat"
etc. (but not "demo.com"
since c
is in the file's extension); you can try *c*.*
wild card: file name contains c
with any extension:
string[] dirs = Directory
.GetFiles(@"c:\", $"*{filetofind}*.*");
In case of elaborated condition, when standard wildcard in not enough, just add Where
:
string[] dirs = Directory
.EnumerateFiles(@"c:\", "*.*")
.Where(path => Your_Condition(Path.GetFileNameWithoutExtension(path)))
.ToArray();
For instance, let's test file name for small (not capital) letter c
string[] dirs = Directory
.EnumerateFiles(@"c:\", "*.*")
.Where(path => Path.GetFileNameWithoutExtension(path).Contains('c'))
.ToArray();