Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file write permission issue under "Program Files" folder

I am using inno setup to make a installation package for my application, and my application is written by C# + .Net 2.0 + VSTS 2008. Inno setup => http://www.jrsoftware.org/isinfo.php and I install my application under Program Files/Foo folder (Foo is my application name). My application is targeting to Windows Vista.

The issue I found is my program cannot write to the folder Program Files/Foo. And I need the permission of write to this folder in order to save some configuration files. The strange thing I notice is the folder Program Files/Foo is marked as readonly and I have checked all folders under Program Files are marked with read only, such as Office.

My questions are,

  1. Why all folders are marked as read only under Program Files? It means we should not write to individual application folders under Program Files? If not, where should we write information to disk like user last selected configuration information of an individual application?
  2. If we could write to individual application folders under Program Files, what is the solution? I do not want my application to Run As administrator to solve this issue, and if there are solution to write to this folder, I want to require minimal permission if possible.
like image 624
George2 Avatar asked Jul 28 '09 04:07

George2


People also ask

Why can't I change permissions for Program Files?

Fix PermissionsRight-click on Program Files. Select Properties then the Security tab. Click Advanced and click Change Permissions. Select Administrators or your account.

How do I give myself permission to save in Program Files?

Step 1: Right-click the folder you want to save files to and select Properties from the context menu. Step 2: Select Security tab in the pop-up window, and click Edit to change permission. Step 3: Select Administrators and check Full control in Allow column. Then click OK to save the changes.


2 Answers

You should write user specific config data to the Application Data folder for the current user, using the special folders enum and the Enivronment.GetFolderPath.

like image 118
Paul van Brenk Avatar answered Sep 21 '22 18:09

Paul van Brenk


Best Practice is to not store config data in the Program Files folder. Instead, store your application's data in %AppData%\YourApplicationName. Depending on whether you want to store your config data per-user or in a shared common folder, use one of the following enums to get the folder path:

string userAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);  
string commonAppData = Envrionment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 

By default, Vista users do not run programs as Administrators and hence those programs have only read access to the folders under "Program Files". Users can change this behavior by disabling UAC and you could ask your users to do that, but in an office setting users might not have that option. That's why you use AppData instead -- applications can always read and write data to the AppData folder.

Information on UAC can be found at Microsoft's site. Although this page is fairly long, it's a starting point for understanding UAC: http://msdn.microsoft.com/en-us/library/bb530410.aspx

like image 25
AndrewS Avatar answered Sep 19 '22 18:09

AndrewS