Take the 2-minute tour ×

This snippet works well if I try to write in a user directory but as soon as I try to write in Program Files, it just executes silently and the file has not been copied (no exception). If I try to copy the file in C:\ or in C:\Windows I catch an UnauthorizedAccessException.

Do you know another way to get the permissions to write in that directory or to make it work another way?

Any help greatly appreciated! Thanks

using(FileStream fs=File.Open(source, FileMode.Open)){ }  
    FileIOPermission fp = new FileIOPermission(FileIOPermissionAccess.Write,   
                          AccessControlActions.Change, "C:\\Program Files\\MyPath");  
    fp.Demand();  //<-- no exception but file is not copied
    File.Copy("C:\\Users\\teebot\\Documents\\File.xml","C:\\Program Files\\MyPath\\File.xml",true);  
catch(UnauthorizedAccessException unauthroizedException)
    throw unauthroizedException;
share|improve this question
When rethrowing an exception, just use throw; - throw s; will obliterate the stack trace. –  Mark Brackett Oct 1 '08 at 21:45

4 Answers 4

up vote 1 down vote accepted

If you are running under Vista then the system just redirects writes to the program files folder, this is done so old program that keep their configuration in the program directory will continue to work when the user is not an Admin (or UAC is enabled).

All you have to do is add a manifest to your program that specify the required access level, then the system assume your program is Vista-aware and turns off all those compatibility patches.

You can see an example of a manifest file on my blog at:


(the focus of the post is on getting the right version of the common controls, but the Vista security declarations are also there)

share|improve this answer

Don't write in the Program Files folder.

That's a big no-no, and will especially cause problems when the day comes where your code runs in Vista or on a machine at a company where users only get standard security rather than admin rights. Use the Application Data folder instead.

share|improve this answer

Are you running on Vista? If so then you may be running into file system virtualization. This is a feature in 32 bit versions of Vista which allows a normal user to write to protected parts of the file system. It's a shim introduced to reduce the pain of the LUA features of Vista.

The short version is that the operating system will create a virtual file system for certain protected roots (such as program files). When a non-admin attempts to write to it, a copy will be created an editted instead of the original. When your user account attempts to look at the file it will see the edit.s Other user accounts will only see the original.

Longer Version: http://thelazyadmin.com/blogs/thelazyadmin/archive/2007/04/26/file-system-virtualization.aspx

share|improve this answer

Code access security grants or denies permissions to your code. It can't be used to override permissions that are granted/denied to the current user.

share|improve this answer

Your Answer


