简体   繁体   中英

Access to path **** is denied

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.

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.

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
}

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

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?

PS Check if your file is not already opened by another FileStream or etc.

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());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM