How to do a file search?  
Author Message
Ke Sun





PostPosted: Visual C# General, How to do a file search? Top

Hi,

I would like to ask how can I implement a file search on a local or network drive I have read about implementing a recursive search. I would also like to use wild cards too. Once the program is done, it should have the same functions as “Windows Search Companion”.

Is there any API available for doing search Any suggestions or code examples will be wonderful; I am fairly new to C#. Thank you very much!



Visual C#7  
 
 
Peter Ritchie





PostPosted: Visual C# General, How to do a file search? Top

The GetFiles methods are what you're looking for. For example:


FileInfo [] files = dir.GetFiles("*.txt",SearchOption.AllDirectories);

or



 
 
StLuisRey





PostPosted: Visual C# General, How to do a file search? Top

Here is a recursive function to find files based on a given file mask. You will have to modify it to do exactly what you want but it is a start.....

static void search(String path, String pattern, bool printFiles)
{
String[] dirs = System.IO.Directory.GetFiles(path,pattern);
String[] files = System.IO.Directory.GetFiles(path,pattern);

foreach(String file in files)
{
if(printFiles)
Console.WriteLine(file);
}


dirs = System.IO.Directory.GetDirectories(path);

foreach(String dir in dirs)
search(dir,pattern,printFiles);

}