异常文件夹:访问路径被拒绝

Exception Folder: Access to the Path is denied

我正在使用以下代码访问 4 个文件夹。但是,有 1 个文件夹是私有的,只有老年人可以访问它。我怎样才能避开该文件夹并仍然访问其他 4 个文件夹?

class Program
{
    const string FILENAME = @"H:\Personal\text.xml";
    const string FOLDER = @"F:\Apps";
    static XmlWriter writer = null;
    static void Main(string[] args)
    {
        XmlWriterSettings settings = new XmlWriterSettings
        {
            Indent = true
        };

        writer = XmlWriter.Create(FILENAME, settings);
        writer.WriteStartDocument(true);

        DirectoryInfo info = new DirectoryInfo(FOLDER);
        WriteTree(info);

        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();
        Console.WriteLine("Enter Return");
        Console.ReadLine();
    }

编辑:(WriteTree 代码) 我添加了其余代码,其中还包括 WriteTree 代码。 我没有访问管理文件夹的权限。

    static long WriteTree(DirectoryInfo info)
    {  
    long size = 0;
        writer.WriteStartElement("Folder");
        try
        {
            writer.WriteAttributeString("name", info.Name);
            writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
            writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
            writer.WriteAttributeString("date", info.LastWriteTime.ToString());


            foreach (DirectoryInfo childInfo in info.GetDirectories())
            {
                size += WriteTree(childInfo);
            }

        }
        catch (Exception ex)
        {
            string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
            Console.WriteLine(errorMsg);
            writer.WriteElementString("Error", errorMsg);
        }

        FileInfo[] fileInfo = null;
        try
        {
            fileInfo = info.GetFiles();
        }
        catch (Exception ex)
        {
            string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
            Console.WriteLine(errorMsg);
            writer.WriteElementString("Error", errorMsg);
        }

        if (fileInfo != null)
        {
            foreach (FileInfo finfo in fileInfo)
            {
                try
                {
                    writer.WriteStartElement("File");
                    writer.WriteAttributeString("name", finfo.Name);
                    writer.WriteAttributeString("size", finfo.Length.ToString());
                    writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                    writer.WriteEndElement();
                    size += finfo.Length;
                }
                catch (Exception ex)
                {
                    string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                    Console.WriteLine(errorMsg);
                    writer.WriteElementString("Error", errorMsg);
                }
            }
        }

        writer.WriteElementString("size", size.ToString());
        writer.WriteEndElement();
        return size;

    }
  }
 }

错误: 异常文件夹:错误:拒绝访问路径。

您可以在使用 richardwiden 的回答写入文件之前检查权限:Checking for directory and file write permissions in .NET

或者您可以在 WriteTree() 函数中使用 try catch 来忽略 UnauthorizedAccessException 异常,例如:

try {
    writer.WriteAttributeString("name", info.Name);
    [...]
} catch (Exception ex) {
    // If is a permission error, ignore exception
    if (ex is UnauthorizedAccessException)
         return size;

    // custom error log
    string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
    Console.WriteLine(errorMsg);
    writer.WriteElementString("Error", errorMsg);
}