【问题标题】:search in restricted access folders在受限访问文件夹中搜索
【发布时间】:2014-05-30 08:22:42
【问题描述】:

我想列出我的程序可以访问的所有文件和文件夹,并将它们写入文本文件。我写了一个方法来帮助我做到这一点,但在某些情况下(文件夹被拒绝访问)我的程序停止工作。 我在这里搜索了很多,发现一些链接说使用 try/catch 等等,但我还不能解决我的问题。

string spcdirectorypath = @"C:\Users";
            string spcfiletape = "*.*";
            DirectoryInfo d = new DirectoryInfo(spcdirectorypath);//Assuming Test is your Folder
            FileInfo[] Files = d.GetFiles(spcfiletape, SearchOption.AllDirectories); //Getting Text files
            foreach (FileInfo file in Files)
                {
                    string str = file.FullName + "\n";
                    richTextBox1.AppendText(str);
                }

现在我该如何解决这个问题?或者在其他方面我可以访问那个受限制的文件夹吗?抱歉这个重复的问题并感谢您提供最佳答案:)?

【问题讨论】:

    标签: c# restriction


    【解决方案1】:

    如果您想避免未经授权的访问异常,您应该在尝试列出文件之前检查权限。

    我在这里适应你的情况 the example of MSDN 关于遍历目录树:

    void YourMethod()
    {
       string spcdirectorypath = @"C:\Users";
       DirectoryInfo d = new DirectoryInfo(spcdirectorypath);
    
       WalkDirectoryTree(d)
    }
    
    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)
        {
            // You may decide to do something different here. For example, you 
            // can try to elevate your privileges and access the file again.
        }
    
        catch (System.IO.DirectoryNotFoundException e)
        {
            // You may decide to do something different here. For example, you 
            // can log soething.
        }
    
        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().
                string str = fi.FullName + "\n";
                richTextBox1.AppendText(str);
            }
    
            // 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);
            }
        }            
    }
    

    【讨论】:

    • 感谢您的回复。我对其进行了测试,但出现了此错误:mscorlib.dll 中发生了“System.UnauthorizedAccessException”类型的未处理异常附加信息:对路径“C:\Users\UpdatusUser\Templates”的访问被拒绝。
    • 查看我的编辑,您的原始代码的第二个问题是您通过SearchOption.AllDirectories 一次调用就获得了整个结构
    • 你的新问题帮助了我,我解决了这个问题,对此我非常感激。只是代码中的一个错误:“static void WalkDirectoryTree”应更改为“private void WalkDirectoryTree”以接受richTextBox1.AppendText(str)。这段代码对于计算来说太重了,系统会进入无响应模式,但我将使用 Thread 方法来解决这个问题。无论如何,现在我的程序正在运行,我说谢谢你;)
    【解决方案2】:

    您运行程序的进程应具有所需的权限,否则您将无法访问该文件夹并枚举文件。在您的测试环境中,请确保您以管理员身份运行此代码。

    【讨论】:

    • 我现在以这种方式测试(以管理员身份运行),但问题没有解决。
    • 有些文件夹是您根本无法访问的,除非没有卷影副本(这有点矫枉过正)。例如,System Volume Information。这与权限无关,在某种程度上更像是并发。
    • 然后在这种情况下打开命令提示符 [以管理员身份] 并尝试访问该文件夹并列出该文件夹的内容。如果您无法访问文件夹或列出文件,则说明您没有足够的权限。这是有道理的,否则每个人都会访问彼此的文件。
    • 让我们了解有关您的计划的更多详细信息。 Windows 桌面应用程序?网络应用程序?
    • @alex.pulver C# 中的 Windows 桌面应用程序和 C:\Users\UpdatusUser\Templates 等地址中的程序强制错误
    【解决方案3】:

    跳过那些目录。如果失败,忽略:

    try
    {
      foreach (var file in Directory.GetFiles(spcdirectorypath))
      {
        richTextBox1.AppendText(file + "\r\n");
      }
    }
    catch (IOException)
    {
      richTextBox1.AppendText("Failed " + spcdirectoryPath + "\r\n");
    }
    

    如果异常不是 IOException,只需将其更改为实际抛出的异常。

    【讨论】:

      【解决方案4】:

      我猜 UpdatusUser 是那台机器上的用户。用户文件夹具有受限权限。尝试在 UpdatusUser 下运行您的应用程序。如果您不使用 UpdatusUser 运行您的应用程序,您可以尝试为您的用户设置 UpdatusUser 文件夹的权限(右键单击文件夹 Properties/Security)。

      如果您仍然遇到问题,请更深入地了解使用 Process Monitor 阻止的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 2018-07-05
        • 1970-01-01
        • 2011-06-07
        • 2011-04-16
        • 1970-01-01
        相关资源
        最近更新 更多