I want to find all the document files from D drive . I have written the following code but it is too time consuming. Also due to more time consumption in fetching the data it gives me exception. In order to minimize the time ,I just want to check like this :

even if one of the types of file exist within that folder ->return the path of the folder

so that it doesn't need to check all the type of files within that same folder example:-

if C:\Program Files\Notepad++ has readme.txt ,and other text files. The moment it detects the first text file in this folder it must return : C:\Program Files\Notepad++

Code that I have used in my file

 List<string> mediaExtensions = new List<string>{"txt", "pdf"};
    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);
       }
    }

Update1: Checking this link did gave me an idea of recurssion and ignoring the files but not minimizing the time taken to search the files.

Update2:- In the above code that I used if(mediaExtensions.Contains(Path.GetExtension(f).ToLower())) I dont get any value even if there exists files within the folder

4

There are 4 best solutions below

0
On

You can use the Directory.GetFiles() method to find files recursively. Check the options, you can adapt it to fit your needs without iterating the directories yourself:

https://msdn.microsoft.com/en-us/library/ms143316.aspx

0
On

Use this:

foreach (string f in Directory.GetFiles(d, "*.txt")) 
{
    // return ".txt file found in d
9
On
List<string> mediaExtensions = new List<string>{"txt", "pdf"};
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);
           //you said you also need the path of the directory, add it from here
           break;
    }
    DirSearch(d);
   }
}

When you add the f argument, try to also add the d one from the outer foreach, your requirement was to add the folder with at least one file with the desired extension exists.

EDIT 1

When you encounter Documents and settings you have to access it using

var mydocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
5
On

First Solution: (.Net 4.0)
This could speed things up a bit:

    DirectoryInfo dirInfo = new DirectoryInfo(myBaseDirectory);
    return dirInfo.EnumerateDirectories()
           .AsParallel()
           .SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories));
}

Second Solution: (.Net 3.5 and 4.0)
If that`s not helping check out the Faster Directory Enumerator based on a WinAPI function

Sources: http://www.codeproject.com/Articles/38959/A-Faster-Directory-Enumerator
Win Api Function: https://msdn.microsoft.com/en-us/library/aa364428%28v=vs.85%29.aspx