Click here to Skip to main content
Click here to Skip to main content

'Unauthorized Access Exception' when trying to delete folder created from code

By , 7 Jan 2011
 
'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.
//Code to remove readonly attributes of folder before delete

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(files, new Action(f =>
                                {
                                  File.SetAttributes(f.FullName, FileAttributes.Normal);
                                }));
 
Folder.Delete(true);
 
Hope you find this useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anupama Roy
Software Developer (Senior) Dell
India India
Member
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:

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 Thanks for sharingmemberlinuxjr11 Jan '11 - 8:46 
Reason for my vote of 5
Thanks for sharing
GeneralReason for my vote of 5 Nice share!mvpSandeep Mewara8 Jan '11 - 7:03 
Reason for my vote of 5
Nice share!
GeneralHave a look at this link: http://www.codeproject.com/script/...mvpSandeep Mewara5 Jan '11 - 8:35 
Have a look at this link: http://www.codeproject.com/script/Articles/BlogFeed.aspx
See, if you find any more issues.
 
Menu bar top-> Articles -> Post your Blog
GeneralThank you Sandeep for your valuable comments.I need some hel...memberAnupama Roy5 Jan '11 - 6:28 
Thank you Sandeep for your valuable comments.I need some help to post blog entries over here.
GeneralI have updated your Tip/Trick based on what you were trying ...mvpSandeep Mewara5 Jan '11 - 5:18 
I have updated your Tip/Trick based on what you were trying to convey.
 
I have suggestion though. Either you post it completely over here (with full details as in your blog) OR post your blog entry directly.
Posting a Tip/Trick and referring a blog is kind of confusing. Pick one and make it full. If you want, you can post as blog entry too here at CP.
 
Do tell me, if you need help.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by Anupama Roy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid