【问题标题】:How can I skip a non-accessible folder?如何跳过不可访问的文件夹?
【发布时间】:2013-04-10 02:20:32
【问题描述】:

这是我的递归函数:

private void createListFolder(string path)
{
    try
    {
        string[] directoryList = Directory.GetDirectories(path);
        if (directoryList.Length > 0)
        {
            foreach (string directory in directoryList)
            {
                if (Directory.EnumerateFiles(directory).Any())
                {
                    folderList.Add(directory);
                }

                createListFolder(directory);
            }
        }
    }
    catch (Exception err)
    {
        txtErrors.AppendText(err.Message + Environment.NewLine);
    }
}

我从根路径开始调用,带有文件的只读文件夹并将路径添加到列表中。问题是,如果我捕捉到一个不可访问的文件夹,它就会停止。我只想忽略不可访问的文件夹(或者更好的是,得到一个异常,但仍然读取其余文件夹)。

我该怎么做?

【问题讨论】:

    标签: c# exception permissions tree directory


    【解决方案1】:

    如果您在循环中创建第二个 try/catch(嵌套的 try/catch 语句),则会在此处捕获错误,然后循环将继续。

    【讨论】:

      【解决方案2】:

      试试下面的方法会对你有所帮助...

      try
      {
       createListFolder(directory);
      }
      catch (Exception err)
      {
       txtErrors.AppendText(err.Message + Environment.NewLine);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-02
        • 1970-01-01
        • 2018-10-21
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多