Search code examples
c#access-denied

Exception Folder: Access to the Path is denied


I am using the following code to access 4 folders. However, there is 1 folder which is private and only seniors have access to it. How can I avoid that folder and still access the other 4 folders?

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

EDIT: (WriteTree Code) I have added the rest of the code which also includes the WriteTree code. I do not have access to the management folder.

    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;

    }
  }
 }

Error: Exception Folder: Error: Access to the path is denied.


Solution

  • You could check the permissions before write the file with richardwiden's answer : Checking for directory and file write permissions in .NET

    Or you could use try catch in your WriteTree() function to ignore the UnauthorizedAccessException exception like :

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