locked
Alternative to using Directory.GetFiles(path, mask, SearchOption.AllDirectories) function. RRS feed

  • Question

  • I new in C# and I'd like to use the Directory.GetFiles(path, mask, SearchOption.AllDirectories) function but with VS 2013 and Windows 8.1 I get and access error when it tries to access the Recycle Bin while searching the C:\ Drive for mp3's. I'd imagine there are other directories I'll get an access violation with as well. I've looked all over the net for alternatives but I simply don't understand them and I'm not sure that they search an entire drive disregarding the access violations. Can anyone give me an example in C# allowing me to search a drive or any directory, for certain types of files while avoiding access errors? If so, the biggest thing I need is an explaination to learn from and an example on how to use it. Thanks so much.


    • Edited by _Neil Sunday, May 11, 2014 1:42 PM
    Sunday, May 11, 2014 1:40 PM

Answers

  • Hi, here is the example :

    string[] SafeGetFiles(string path, string mask)
    {
        List<string> fileList = new List<string>();
    
        try
        {
            foreach (string folder in System.IO.Directory.GetDirectories(path))
            {
                fileList.AddRange(SafeGetFiles(folder, mask));
            }
        }
        catch
        {
        }
    
        try
        {
            fileList.AddRange(System.IO.Directory.GetFiles(path, mask, System.IO.SearchOption.TopDirectoryOnly));
        }
        catch
        {
        }
    
        return fileList.ToArray();
    }

    • Marked as answer by Fred Bao Tuesday, May 20, 2014 2:31 AM
    Sunday, May 11, 2014 2:55 PM

All replies

  • Hi, here is the example :

    string[] SafeGetFiles(string path, string mask)
    {
        List<string> fileList = new List<string>();
    
        try
        {
            foreach (string folder in System.IO.Directory.GetDirectories(path))
            {
                fileList.AddRange(SafeGetFiles(folder, mask));
            }
        }
        catch
        {
        }
    
        try
        {
            fileList.AddRange(System.IO.Directory.GetFiles(path, mask, System.IO.SearchOption.TopDirectoryOnly));
        }
        catch
        {
        }
    
        return fileList.ToArray();
    }

    • Marked as answer by Fred Bao Tuesday, May 20, 2014 2:31 AM
    Sunday, May 11, 2014 2:55 PM
  • Thanks for the reply. I used...

    string[] files = SafeGetFiles(folderBrowserDialogMain.SelectedPath, "*.mp3");

    How do I found out how many files were found in string[] files?



    • Edited by _Neil Sunday, May 11, 2014 4:28 PM
    Sunday, May 11, 2014 4:26 PM
  • Thanks for the reply. I used...

    string[] files = SafeGetFiles(folderBrowserDialogMain.SelectedPath, "*.mp3");

    How do I found out how many files were found in string[] files?



    files.Length property will tell you how many files there. 
    Sunday, May 11, 2014 4:56 PM
  • Thanks! I was trying to use files.count<>.
    Sunday, May 11, 2014 5:13 PM