2

I'm creating a WIX installer for a C# application. In the application I use System.Configuration.ConfigurationManager.AppSettings[Setting1] to get settings.

My question is, where must I place the program.exe.config file on the machine in order for it to work? I can't place it with the program in ProgramFiles directory, since those files are read-only.

I tried: Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); config.AppSettings.Settings[Config1].Value = "Value1"; config.Save();

Unfortunately I don't know where it's looking.

Thanks your replies, Trevy

Trevy Burgess
  • 499
  • 5
  • 12
  • Just to help you out my answer to write to config files http://stackoverflow.com/questions/289773/how-to-modify-net-config-files-during-installation/290374#290374 – CheGueVerra Feb 03 '15 at 17:06

3 Answers3

1

It should be in the same folder of the program.exe. Use wix to copy both program.exe and program.exe.config to the required folder.

If you need to edit the configuration file during the installation you will need to do it using a custom action in wix. Make sure you pass the file path to the custom action and write the code to read the configurations in that file and edit.

LeoN
  • 1,590
  • 1
  • 11
  • 19
0

The problem isn't that the files there are readonly - the problem is that you are trying to update files in the Program Files folder with your config.Save, and you can't do that if you are a limited user, and you are always limited (even if you are admin) unless you elevate. The short answer is that:

  1. If your app routinely updates files in restricted areas then it probably needs elevation, so give it an elevation manifest.

  2. If however you require you app to be used by limited users and allow them to update that config file then don't install to Program Files. Choose User Appdata folder, for example. Windows is probably using your config file during program startup, so you can't separate it from the exe.

PhilDW
  • 20,260
  • 1
  • 18
  • 28
-1

When I was creating an installer for my app, I found I couldn’t save my settings.

The reason is because the Program Files repository, from a practical point of view, is read-only (Applications should never run with elevated permissions). When installing a program, the only time we modify the MyApp.exe.config file is at installation/upgrade/repair time.

The config file has many sections. One of them is userSettings. This is where we store any data we need to modify during the lifetime of the application. When run for the first time, Windows creates a hidden file in the user’s AppData folder. That is why we can save user settings, even though the config file is in the same directory as the MyApp.exe

So the answer is, if we run into permission errors when trying to save our settings it means we are writing our settings to the wrong section of the config file. We need to place it in the userSettings section and nowhere else.

For convenience, Visual Studios has a settings editor (the settings tab of the properties page). It allows you to create strongly typed user and application settings. The generated class lets you save user settings, but not application settings for the above reasons.

Trevy Burgess
  • 499
  • 5
  • 12