file write permission issue under "Program Files" folder - c#

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,
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?
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.

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.

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

A common solution would be to install configuration files to the Application Data folder i.e. like follows:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Related

Why my .exe file doesn't work when I install the program?

I have a problem when I install my Windows Forms App in a computer. The .exe file doesn't do anything when I click it. To start the aplication, I have to run the program as administrator.
It is developed in C#, .Net Framework 4.7.2 and I install it in a computer with Windows 10.
THanks!
You should not save anything to the application folder i program files. You should use a separate folder for any data that might change. There are a few locations to chose from:
ProgramData contains application data that is not user specific.This data will be available to all users on the computer. Any global data should be put in here.
AppData folder contains configuration settings, downloaded
information/files for a particular user. So, for example any user
specific preferences and profile configurations can be stored in the
AppData folder. The AppData folder is further divided into three
subfolders
Roaming - This folder contains data that can move with your
user profile from a computer to another.
Local - This folder contains
data that will not move with your user profile.
LocalLow - You can put
in lowlevel access information such as information related to web
browser running in a protected mode in this folder.
source
The location for these folders can be accessed by using Special Folders
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

C# app.config files and UAC

I started out with my application writing the config to the registry like I did years back with UAC wasn't around.
Turned out this wasn't a good idea so I moved to the app.config file and found that this is having issues as well with UAC.
The configuration I have for the application is machine specific; individual users do not have their own configuration and the rest of the apps including the service all drive off the single configuration file.
If I try updating this file in the program files\myapp folder it gets denied.
What is everyone else doing for a scenario like this where there needs to be a single configuration file?
I should also mention that this configuration needs to be updated from within my applications.
I would use the Common Application Data folder, this is where most new applications store data which works with UAC.
You can get this with the following code:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
To view all options for special folders you can check this link:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
The most common options would be the following:
Application Data - The directory that serves as a common repository for application-specific data for the current roaming user.
Common Application Data - The directory that serves as a common repository for application-specific data that is used by all users.
Local Application Data - The directory that serves as a common repository for application-specific data that is used by the current, non-roaming user.
You should be storing your users' settings in app.config you should be using user.config. Have a look here for more info.
Any machine-wide configuration files should be stored in:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Files in Program Files shouldn't be modified except when the application is installed/updated.
I had a similar need with a WPF application. What I do is the following:
Install the application with a boiler-plate configuration file.
When the app runs, it checks for a config file in the user's App Datadirectory for configuration files, if they're not present it copies the default boiler-plate config files from the install directory.
I then do any first-run initialization stuff that is needed.
I started out using the .NET ConfigurationManager, but I switched over to a plain old XML files because it was simpler and my requirements were basic.

Issue Related to Read only DB file after installation

I have an Application in WinForms,
I have created SetupDevelopment project and then install an application using Setup.exe file
i have added MSAcceess db file into application folder file,
when i install an application the database file gets readonly. how can i make file writtable after installation?
I found some solution on internet and found that when i tried to install application in different path other than "C:\ProgramFile\" and make application available to all user,it solves my Problem, but i want to install an application only in "C:\ProgramFile\" and want to available to only "Current user"
how can i achieve my Problem ?
In modern Windows Operating System the folders 'Program Files' or 'Program Files (x86)' are read only for the common users. This choice has been done for security reasons and you cannot easily bypass this rule. The reccomended folders, to use just for the current user, are C:\users\username\AppData\Roaming or C:\users\username\AppData\Local identified by the Environment.SpecialFolder.ApplicationData or Environment.SpecialFolder.LocalApplicationData. To this base folders add another subfolder identifying your application or company and install your database there.
If you want your database available to all users of the current PC, you could install it in the Environment.SpecialFolder.CommonApplicationData that resolves to C:\programdata. (Again, adding a subfolder specific for your application or company)
Again, don't try to force the operating system to work against its rules. You will find yourself in trouble very often.

Rewriting the xml file in my application folder

Am using WPF-C#, when am closing my application i need to write some xml document. its located in my application folder (../Data/test.xml). its working while debuging. when i deploy my application cant able to access the xmlfile in the program files folder. how i can resolve this issue.. the following image is the exception http://img197.imageshack.us/i/errqu.jpg/
Your users cannot modify a file located in Program Files.
If you have a configuration file you want to modify, you need to store it in the user's own ApplicationData folder, e.g.:
C:\Users\Bill\AppData\Local\SoftCo Software Inc\SuperApp\appconfig.xml
If you want standard users to be able to modify this config file, then your installation program is supposed to grant Everyone Full Control to the file. Your installation program is known to be running as an administrator, so it will have permissions to change permissions.
But you should not do that; this allows one user to alter program settings that will affect another user. These settings should be per-user (and stored in %AppData%, which the user already will have access to).
It is because of Windows UAC. You can fix the issue either
using Run As Administrator option, from right click menu in the application.
Modify the AppManifest file.
Change the file location user writable locations.

Access to SQL CE database in Program Files directory

I've read a bit that putting a.NET 2.0 SQL CE database in the program files directory where the application is installed is a bad idea. I'm running into the following error:
Access to the database file is not allowed. [ File name = C:\Program Files (x86)\MyCompanyName\MyApplicationName\MyCEDatabaseName.sdf ]
So I've heard, put it in Common Application files.... I'm not sure where that is, and I've read that the installer may drop the file there with the same permissions. I've also read that people have moved their file there and had issues.
Is it possible to change the ACLs of the file in the program files directory when the installer drops it there? I already have hooks into the application from the MSI to collect and set configuration options. I haven't found a good example of how to change the permissions on ONE file.
Then I've read to store it in the user's application data, but if you're installing the application 'for everyone' where will the file go?
Thanks for your time.... confused.
I assume you are using a MSI package. In this case you can use CommonAppDataFolder which is resolved to "C:\ProgramData".
However, this is also a per-machine location, so it requires the same privileges as Program Files. I think you heard about Application Data folder (AppDataFolder) which is a per-user location (something like "C:\Users\\AppData\Roaming"). It doesn't have permission problems.
If you want to use a per-machine location a solution is to use the LockPermissions table:
http://msdn.microsoft.com/en-us/library/aa369774(VS.85).aspx
This is a bit complicated because of all the permission flags. So if your setup authoring tool doesn't offer support for setting permissions, I suggest a simpler approach: use xcacls.exe as a custom action:
http://support.microsoft.com/kb/318754
This way all users can gain access to your database file.

Categories