8

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.

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
user589195
  • 4,180
  • 13
  • 53
  • 81
  • Have you tried saving it to the root of C or another folder? It could be an issue with UAC and permissions on users folders - is this on Win7 or Vista? – Charleh May 18 '12 at 10:02
  • Try running the app as admin rather than visual studio. – Bali C May 18 '12 at 10:04
  • 1
    what filename are you specifying? – Rich May 18 '12 at 10:26
  • 2
    Is `ConfigOutput` the name of a directory or the filename you want to use? If it's an existing directory, this is the error I get as well. You need to include a filename in your path. – anton.burger May 18 '12 at 10:30

5 Answers5

12

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.

H H
  • 263,252
  • 30
  • 330
  • 514
4

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
}
FosterZ
  • 3,863
  • 6
  • 38
  • 62
4

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

Rich
  • 3,781
  • 5
  • 34
  • 56
2

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.

Community
  • 1
  • 1
sickUnit
  • 319
  • 2
  • 5
1

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());
    }
Davio
  • 4,609
  • 2
  • 31
  • 58