Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to path **** is denied

Tags:

c#

I know there is a ton of stuff on this already and have tried a few things but have had no luck in fixing it.

I have a C# program that has built an XML Document and im trying to save it to a folder thats in MyDocuments. I am getting the folliwing exception when calling the XMLDoc.Save function.

Access to the path 'C:\Users\Ash\Documents\ConfigOutput' is denied

I have visual studio running as administrator. Any thoughts on how to fix it?

I have tried saving onto the desktop and into the C:\ folder aswell.

I am using windows 7.

Running the built executable also doesnt seem to work.

Apologies guys it seems I was being stupid. I had indeed not added a filename to the output path. I'll not delete the question incase anyone else gets done by this gotcha! Thanks for all the help/comments.

like image 991
user589195 Avatar asked May 18 '12 09:05

user589195


2 Answers

There are a few possibilities:

  • ConfigOutput is a folder
  • ConfigOutput is a file that is in use (opened)
  • You're not logged in as User 'Ash'

You should not normally have to run as Admin to write to your own Documents folder.

like image 84
Henk Holterman Avatar answered Sep 29 '22 23:09

Henk Holterman


You need to check and get permission to that directory/file your writing.. for that use Security namesapce

var permissionSet = new PermissionSet(PermissionState.None);    
var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, pathToFolder);
permissionSet.AddPermission(writePermission);

if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
{
    // do your stuff
}
else
{
    // alternative stuff
}
like image 36
FosterZ Avatar answered Sep 29 '22 23:09

FosterZ