views:

220

answers:

4

I am developing an application that saves its settings in the install folder. If I install the app in the Program Files on drive C: and attempt to write the settings file I get an exception and that's it.

I know that the User Account Control (UAC) migth be the one that is not letting my app modify the content of the file.

I need help with the following issues:

  • Do the file editing in such a way that at least an UAC warning should be shown and if I answer yes the file becomes writable
  • If there is no way to edit the file on drive C: I need a method to store data somewhe

A more generic question would be:

How to create a C# program that after installing it to C:\Program Files\MyProgram under Windows Vista can manipulate (create/edit/delete) an .ini file in the installation directory? This file should be the same for all users.

+9  A: 

Why don't you store the settings in a user-specific location like C:\Users\Username\AppData?

That way different users can have different settings on the same machine. Also, this is the recommended location for settings and the like.

Ben S
+1, When in Rome.
sixlettervariables
But _please_ don't hardcode that path.
Henk Holterman
@Henk: Yes please, I didn't mean to imply doing that is a good idea.
Ben S
You can use SHGetFolderPath() to get that path.
asveikau
+5  A: 

Building on the answer from Ben S, check out the Environment.GetFolderPath method.

This method allows you to abstract away the specific location and just use a known SpecialFolder path instead (ie SpecialFolder.ApplicationData).

akmad
+1  A: 

I'm surprised it hasn't been mentioned yet, but a viable C# option is to ditch INI files (yuck) and embrace the Settings facilities provided by .Net. They work very well across all Windows versions, they are directly supported by Visual Studio, and finally they are overridable at both the User and Machine level.

We've had no real problems to speak of utilizing this feature (this includes XCopy deployments, Installed applications, Citrix, etc).

sixlettervariables
I totally agree with you, but my case is special: I HAVE to use an .ini and that file has to be the same for all users.
Germstorm
Oy, I've been in that boat a few times, not enviable. I've voted up @Henk's as he has one of the better answers.
sixlettervariables
The settings don't have a good way for writeable _and_ shared data.
Henk Holterman
+1  A: 

Building on the answers of Ben S and akmad, you should put the ini file in the appDataFolder.

If you want the settings to be unique to each user, create an ini file for each user and put it in their AppData folder, which can be retrieved with the following code:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

If you want the settings to be common to all users, but the ini file in the common AppData folder.

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
epotter