views:

2075

answers:

6

Hello,

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)){ }  
try  
{  
    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(SecurityExceptions)  
{  
    throw(s);  
} 
catch(UnauthorizedAccessException unauthroizedException)
{
    throw unauthroizedException;
}
A: 

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.

Joel Coehoorn
A: 

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.

Joe
+1  A: 

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

JaredPar
A: 

To Joel : The code I'm writing will be used to automatically configure Outlook so I don't really have the choice. It has to be in : C:\Program Files\Microsoft Office\Office12\OutlookAutoDiscover

teebot.be
+1  A: 

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:

http://www.nbdtech.com/blog/archive/2008/06/16/The-Application-Manifest-Needed-for-XP-and-Vista-Style-File.aspx

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

Nir
A: 

Thank you Nir. I was not aware of the vista virtualized file system

teebot.be