Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.

I have a VS 2008 solution in which there are 2 projects.
One project is the actual application that outputs myApp.exe and the other project is a VS setup project linked to the first project.

The installer copies myApp.exe and the dependencies in C:\Program Files (x86)\appDir. In this same folder myApp.exe is trying, with no success, to create a new file.

The code in myApp looks something like this:
C#
File.WriteAllLines(
   Application.StartupPath + "myFile.txt",
   new string[]{"test1", "test2", "test3"});

On Windows XP this works just fine.

On Windows 7 I get a System.UnauthorizedAccessException that reads: "Attempt to perform an unauthorized operation". I believe this is because on install the appDir folder is created using Administrator permissions and on run-time myApp.exe tries to write using User permissions.

Q1: How can I modify the setup project so that it can create the appDir with User writing access?

OR

Q2: How can I write the code so that the system allows me to create a new file regardless of access permissions?

Can anyone please help me with this issue?
Thank you!

PS: I've tried this and it fails as well:
C#
DirectoryInfo dInfo = new DirectoryInfo(Application.StartupPath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
FileSystemAccessRule fsar = new FileSystemAccessRule(
                                  WindowsIdentity.GetCurrent().Name,
                                  FileSystemRights.WriteData,
                                  AccessControlType.Allow);
dSecurity.AddAccessRule(fsar);
dInfo.SetAccessControl(dSecurity);
Posted

You should not be doing this. I know a lot of applications did this under XP, but this kind of behaviour forces you to run your app as Administrator under Vista and Weven.
If you want to share data between all users of the application, your installer should query the user for a path where that data should be stored, create the path, and put a reference to it (either in HKLM, or through the App.Settings mechanism) somewhere.
(This is the fastest way of flunking your "certified for Windows 7" exam :-) )
Google articles on folder virtualization in vista, and you'll see what I'm talking about.

The general principle under Vista and Seven is HKLM and \Program Files are read only for normal operations (they were in XP, but everybody was running as administrator).
 
Share this answer
 
Along the lines of Michel's suggestion, you should be using the ApplicationData or CommonApplicationData folders.

If you decide to go this route, this tip[^] may help.
 
Share this answer
 
There are a number of system folders available in Windows Vista/Seven. This article[^] describes what each of the system folders are and where in Windows Vista/Seven the folders map to by default.

Global shared data usually gets mapped to C:\ProgramData, for example. User specific data is usually mapped to C:\Users\[username]\AppData\Local or C:\Users\[username]\AppData\Roaming. If you're maintaining compatibility with Windows XP, it's best to use the %AppData% environment variable rather than the %LocalAppData%, as the %LocalAppData% EV doesn't exist in Windows XP.

To access these locations in .NET, check out Environment.SpecialFolder in the MSDN documentation.

Hope this helps.

Flynn
 
Share this answer
 
There might be a way to do that in code but im not aware how.
What you should do instead is to run your setup project as administrator.
Just right click your setup project and select run as administrator.
 
Share this answer
 
Comments
AugustinJ 19-May-10 14:40pm    
Reason for my vote of 1
All the above answers are right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900