-2

How can I find all executables on hard disk satisfying following conditions 1. Skip to next folder if a folderis not accessible while searching 2. Skip to next folder if a path is too long while searching

Here is my code:

public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
{
    ///meth1
    while (true)
    {
        string[] searchPatterns = searchPattern.Split('|');
        List<string> files = new List<string>();
        try
        {
            foreach (string sp in searchPatterns)
                files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
        }
        catch (UnauthorizedAccessException UAEx)
        {
            MessageBox.Show(UAEx.Message);
            continue;
        }
        catch (PathTooLongException ex)
        {
            MessageBox.Show(ex.Message);
            continue;
        }
        files.Sort();
        return files.ToArray();
    }
}
  • 4
    Add some detail in your question please, ask question in title is not lisible – Alexis Nov 15 '16 at 09:16
  • i need to get all the exe's present in my hard disk. The above code just skips folders with acess issues and long path issues. I need them solved – Martin Sebastian Nov 15 '16 at 09:22
  • 2
    Do you need to access folders with _access issues_? Well, if you do not have permissions then...you do not have permissions (and **fortunately** there is nothing you can do about that). About long paths: http://stackoverflow.com/q/5188527/1207195 – Adriano Repetti Nov 15 '16 at 09:24
  • how to skip to next folder.. for eg : if C://users/demo/abc is a folder and demo doesn'y have access permisison, the current code goes to next folder on Demo.. how to skip to next folder in User – Martin Sebastian Nov 15 '16 at 09:47

2 Answers2

0

You said :

how to skip to next folder.. for eg : if C://users/demo/abc is a folder and demo doesn'y have access permisison, the current code goes to next folder on Demo.. how to skip to next folder in User

Your code doesn't test the rights on the folder, but catches the rights exceptions caused by the forbidden file access. So, you won't be able to go to the next folder with this solution. You need to refactor the entire code.

In addition, your code seems to have an infinite loop when you don't have the rights on a file. So first, I think you should remove the while loop and put your trycatch expression Inside your foreach loop. That way, your solution will skip the forbidden files. (More ! Your code will only do one time SearchPattern.Split)

foreach (string sp in searchPatterns)
    {
        try
        {
            files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
        }
        catch (UnauthorizedAccessException UAEx)
        {
            MessageBox.Show(UAEx.Message);
            continue;
        }
        catch (PathTooLongException ex)
        {
            MessageBox.Show(ex.Message);
            continue;
        }
    }
Marc
  • 392
  • 1
  • 3
  • 11
0

You can get all drives in your hard disk using System.IO.DriveInfo.GetDrives() and iterate all of those. Then use System.IO.Directory.GetFiles() with System.IO.SearchOption.AllDirectories search option to search in sub directories in that drive.

List<string> result = new List<string>();
foreach (System.IO.DriveInfo driveInfo in System.IO.DriveInfo.GetDrives())
{
    result.AddRange(System.IO.Directory.GetFiles(driveInfo.Name, "*.exe", System.IO.SearchOption.AllDirectories));
}
sam
  • 931
  • 2
  • 13
  • 26