tags:

views:

153

answers:

4

Hi, I want to search the media files situated in my system by c#. means I want to create the search engine that will scan all drives (again small question here , how to get the drives on our system by c# code ? )and search the media files like .mp3,.mp4,...etc . How can i do that by c# desktop application?

+1  A: 

try this:

List<string> mediaExtensions = new List<string>{"mp3", "mp4"};
List<string> filesFound = new List<string>();

void DirSearch(string sDir) 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, "*.*")) 
    {
        if(mediaExtensions.Contains(Path.GetExtension(f).ToLower()))
           filesFound.Add(f);
    }
    DirSearch(d);
   }
}
Manu
+2  A: 

Rather than a brute-force iterative search through directories, I recommend looking into using the Windows Desktop Search API, which will be orders of magnitude faster.

http://stackoverflow.com/questions/870101/windows-desktop-search-via-c

richardtallent
A: 

To get your drive list:

string[] drives = Environment.GetLogicalDrives();

To get all your files:

foreach(string drive in drives)
    string[] allFiles = Directory.GetFiles(drive, "*.*", SearchOption.AllDirectories);

To get all your files using recursion:

List<string> allFiles = new List<string>();
private void RecursiveSearch(string dir)
{
    allFiles.Add(Directory.GetFiles(dir));
    foreach(string d in Directory.GetDirectories(dir))
    {
      RecursiveSearch(d);
    }  
}

Filter using Manu's answer

Veer