Getting Unauthorized Access Exception when trying to delete Folder from C#

Posted: January 4, 2011 in File Operations
Tags: , , ,

It took sometime to figure out what went wrong when I was getting an Unauthorized Access Exception when trying to delete folder from C#.

UnAuthorizedAcess Exception when trying to delete a folder

There was a misleading factor that the folder was residing on our server & had an impression that authorization is not given to do so.I started thinking from a code perspective,when I could delete the folder manually & later via code when I manually changed the readonly attribute to false. So the problem was that readonly attribute gets set by itself while folder/file is getting created or copied via code hence it will not let you delete unless you remove the attributes.

I am adding the code snippet to delete the folder with the comments herewith

public void RemoveFolder(string[] FolderNames)
//Gets Path from web.config
string Path = ConfigurationManager.AppSettings[“Root”];
//Loops through all the folder names & form the DirectoryInfo Path
FolderNames.ToList().ForEach(foldername =>

DirectoryInfo Folder = new DirectoryInfo(Path + @”\” + foldername.Trim());
//Checks if folder exists in the Path provided
if (Folder.Exists)
//Sets attributes for the DirectoryInfo
Folder.Attributes = Folder.Attributes & ~System.IO.FileAttributes.ReadOnly;
//Gets all files inside the folder
FileInfo[] files = Folder.GetFiles();
//For each file,set the attributes so that it does not throw
//unauthorized access exception while trying to delete
Array.ForEach<FileInfo>(files, new Action<FileInfo>(
                            f =>
                                File.SetAttributes(f.FullName, FileAttributes.Normal);



Hope you find this post useful 🙂

  1. Prabu says:

    Excellent. Thanks for sharing the code.

    A small suggestion from your code. I am getting an error when I copy and paste it. Once I made the following change, then its fine.

    Array.ForEach(files, f =>
    File.SetAttributes(f.FullName, FileAttributes.Normal);

  2. Anupama says:

    There was a small typo in that code,Thanks a lot for pointing.It’s infact a bug with wordpress that when I put that line of code,it’s not rendering in the exact way.Hence I have removed code formatting & updated 🙂

    Take a look at the modified code.

  3. dharmit says:

    what is wrong with this code then
    if (Directory.Exists(Server.MapPath(“/Images/GalleryImages/”)))
    FileInfo fi = new FileInfo(Server.MapPath(“/Images/GalleryImages/”) + fileName);
    fi.Attributes = FileAttributes.Normal;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s