Reputation: 4240
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.
Upvotes: 8
Views: 45172
Reputation: 273244
There are a few possibilities:
You should not normally have to run as Admin to write to your own Documents folder.
Upvotes: 13
Reputation: 3821
It looks like you're not specifying a filename and therefore it can't create a file with the same name as an existing directory - so try changing your path to:
C:\Users\Ash\Documents\ConfigOutput\Out.xml
Upvotes: 5
Reputation: 3911
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
}
Upvotes: 5
Reputation: 4737
I don't know if this makes a difference, but you may want to specify the folder in a relative rather than absolute manner: Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
will provide you with the path of the current user's documents. Check if it's different from the one you provide.
If so, you may need to run the app under a different user or administrator as others have pointed out. Obviously one user isn't allowed to just save files into another user's documents folder.
For me when I debug my app from Visual Studio the documents folder is the same as the user I'm currently logged in as and running Visual Studio under.
You could try <requestedExecutionLevel
level="asInvoker"
uiAccess="true|false"/>
first and progressively move to highestAvailable
and requireAdministrator
.
Alternatively, demand the permissions, catch the exception and print it out:
try {
FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, myDocFolderFile);
fileIOPermission.Demand();
} catch (SecurityException se) {
Debug.WriteLine(se.ToString());
}
Upvotes: 1
Reputation: 329
Try run your app as administrator.
If you want to debug your app, start your Visual Studio as administrator also.
To force app start as administrator take a look at this thread: How do I force my .NET application to run as administrator?
P.S. Check if your file is not already opened by another FileStream or etc.
Upvotes: 2