需要一种在C#中的所有C驱动器中搜索目录的方法 [英] Need a method for searching all of C drive for a directory in C#

查看:100
本文介绍了需要一种在C#中的所有C驱动器中搜索目录的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件系统中找到一个名为 GameData的文件夹,其通常存储在程序文件中,但是如果没有,我希望能够找到它,无论它在C驱动器中的位置是什么。 / p>

当前,我正在使用:

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

但是它会抛出 UnauthorizedAccessException



也许我可以添加一些内容来阻止它尝试访问任何受保护的目录吗?



编辑是否会在异常后允许 try / catch 阻止搜索继续? >



注意:我不是在寻找特定文件,只是在寻找名为GameData的文件夹,这样我就可以将该位置用作.zip提取的输出,或者读取其中的文件夹名称。

解决方案

您需要使用递归方法而不是 AllDirectories 。然后,您可以跳过导致异常的目录。



MSDN:遍历目录树(C#编程指南)



使用 SearchOption.AllDirectories 的缺点是如果指定根目录下的任何子目录导致 DirectoryNotFoundException UnauthorizedAccessException ,则整个方法将失败并且不返回任何目录使用 GetFiles 方法时也是如此。如果必须处理特定子文件夹上的这些异常,则必须手动遍历目录树

 静态无效WalkDirectoryTree(System.IO.DirectoryInfo根)
{
System.IO.FileInfo [] files = null;
System.IO.DirectoryInfo [] subDirs = null;

//首先,直接处理此文件夹下的所有文件
试试
{
files = root.GetFiles( *。*);
}
//如果甚至其中一个文件需要的权限大于应用程序提供的
//,则抛出此错误。
catch(UnauthorizedAccessException e)
{
//此代码只是写出消息并继续递归。
//您可能决定在这里做一些不同的事情。例如,您$ ​​b $ b //可以尝试提升特权并再次访问该文件。
log.Add(e.Message);
}

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

if(文件!=空)
{
foreach(文件中的System.IO.FileInfo fi)
{
/在这个例子中,我们只访问现有的FileInfo对象。如果我们
//要打开,删除或修改文件,则需要
//一个try-catch块来处理
//自文件删除以来的情况对TraverseTree()的调用。
Console.WriteLine(fi.FullName);
}

//现在找到此目录下的所有子目录。
subDirs = root.GetDirectories();

foreach(子目录中的System.IO.DirectoryInfo dirInfo)
{
//对每个子目录的递归调用。
WalkDirectoryTree(dirInfo);
}
}
}


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.

解决方案

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);
        }
    }            
}

这篇关于需要一种在C#中的所有C驱动器中搜索目录的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆