'Unauthorized Access Exception' when trying to delete folder created from code using C#:
If you have created a folder via code, then folder's readonly attributes gets setup. Now in order to delete it from code, one needs to remove the readonly attribute first. If not done,it will throw an
Unauthorized Access Exception.
If you have a file inside the folder with readonly attribute, then also it will result in same exception.
Folder.Attributes = Folder.Attributes & ~System.IO.FileAttributes.ReadOnly;
FileInfo[] files = Folder.GetFiles();
Array.ForEach(files, new Action(f =>
{
File.SetAttributes(f.FullName, FileAttributes.Normal);
}));
Folder.Delete(true);
Hope you find this useful.
I am a .Net developer working on C#,Asp.net,WCF,WF etc.I would like to utilize this space to share whatever I have come across so far working in .Net so that you can also learn & explore.
I hope you find these posts useful.I’d love to hear from you,so please post in your comments/feedback.
Visit my blog
http://dotnetforyou.wordpress.com/ for more technical articles: