I try to search a file using wild card.My code is:
string SearchQuery ='';
List<ATTFile> lstFiles = new List<ATTFile>();
if (Directory.Exists(FilePath))
{
DirectoryInfo dirInfo = new DirectoryInfo(FilePath);//File PAth is not a problem.
foreach (FileInfo file in dirInfo.GetFiles(SearchQuery + "?"))//Want help here
{
ATTFile obj = new ATTFile();
obj.FileName = file.Name;
obj.Folder = file.Directory.ToString();
obj.Size = int.Parse(file.Length.ToString());
obj.Extension = file.Extension;
lstFiles.Add(obj);
}
}
Code Works if I give full file name.
For Example:
Inside a directory I have following files.
and.jpg
asp.jpg
bb.jpg
cc.jpg
Using above code if I give full file name its work means SearchQuery ="and.jpg".Its work.But If I give SearchQuery ="a" I want a result
and.jpg
asp.jpg
Starts all files with a.Is it possible using wild card inside GetFiles(SearchQuery + "?").Thanks.
You can use the
overloadthat takes the search pattern:If you want all in one query, you can also use LINQ:
Note that i’m using
DirectoryInfo.EnumerateFileshere since it can be more resource-efficient when you are working with many files and directories.