Need a method for searching all of C drive for a directory in C#

我怕爱的太早我们不能终老 提交于 2019-12-10 19:17:58

问题


I need to find a folder called "GameData", in the filesystem, its normally stored in Program Files, but if it is not, i would like to be able to find it, wherever it is in the C drive.

Currently, i'm using:

IEnumerable<string> list = Directory.GetDirectories(root, "GameData", SearchOption.AllDirectories);

But it throws an UnauthorizedAccessException which is expected, as it is trying to navigate through protected directories.

Is there perhaps something i can add to this to stop it from attempting to access any protected directories? Will a try/catch block allow the search to continue after the exception?

EDIT

Note: I'm not looking for a specific file, just the folder called GameData, so that i can use that location as an output for .zip extraction, or to read the names of the folders within.


回答1:


You need to use a recursive method instead of AllDirectories. Then you could skip directories which cause an exception.

MSDN: Iterate Through a Directory Tree (C# Programming Guide)

"The weakness in using SearchOption.AllDirectories is that if any one of the subdirectories under the specified root causes a DirectoryNotFoundException or UnauthorizedAccessException, the whole method fails and returns no directories. The same is true when you use the GetFiles method. If you have to handle these exceptions on specific subfolders, you must manually walk the directory tree"

static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
    System.IO.FileInfo[] files = null;
    System.IO.DirectoryInfo[] subDirs = null;

    // First, process all the files directly under this folder 
    try
    {
        files = root.GetFiles("*.*");
    }
    // This is thrown if even one of the files requires permissions greater 
    // than the application provides. 
    catch (UnauthorizedAccessException e)
    {
        // This code just writes out the message and continues to recurse. 
        // You may decide to do something different here. For example, you 
        // can try to elevate your privileges and access the file again.
        log.Add(e.Message);
    }

    catch (System.IO.DirectoryNotFoundException e)
    {
        Console.WriteLine(e.Message);
    }

    if (files != null)
    {
        foreach (System.IO.FileInfo fi in files)
        {
            // In this example, we only access the existing FileInfo object. If we 
            // want to open, delete or modify the file, then 
            // a try-catch block is required here to handle the case 
            // where the file has been deleted since the call to TraverseTree().
            Console.WriteLine(fi.FullName);
        }

        // Now find all the subdirectories under this directory.
        subDirs = root.GetDirectories();

        foreach (System.IO.DirectoryInfo dirInfo in subDirs)
        {
            // Resursive call for each subdirectory.
            WalkDirectoryTree(dirInfo);
        }
    }            
}



回答2:


You may check How to recursively search directories by using Visual C#:

The GetDirectories and GetFiles methods of the Directory object are all that you need to recursively search for files that match the search string. The following method is used to perform the recursion:

void DirSearch(string sDir) 
{
    try 
    {
       foreach (string d in Directory.GetDirectories(sDir)) 
       {
        foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
        {
           lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
       }
    }
    catch (System.Exception excpt) 
    {
        Console.WriteLine(excpt.Message);
    }
}


来源:https://stackoverflow.com/questions/22223665/need-a-method-for-searching-all-of-c-drive-for-a-directory-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!