0

When I run my WPF application, it creates the user.config file in the following location C:\Users\[user]\AppData\Local\[company]\[applicationName]\1.0.0.0 but I want it to run from a different location, something like C:\Users\[user]\AppData\Local\[company]\UserConfig.

In my App.xaml.cs, I create an OnStartup method, which generates a new user.config file, and I thought the following code would do what I am looking for:

// Open the custom configuration file
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

But seems I was wrong.

Note: configFileMap is my new location and configFile is my user.config.

Any help would be greatly appreciated.

Thank you!

AxleWack
  • 1,801
  • 1
  • 19
  • 51

1 Answers1

0

According to this (https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.openmappedexeconfiguration?view=windowsdesktop-7.0) and this (https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationuserlevel?view=windowsdesktop-7.0) you only have three options (for the directory) to store settings data:

  • in the <exe_name>.config in the application directory
  • in the users local directory you quoted above and
  • in the users roaming directory (parallel to local directory)

This is true for Properties.Settings.Default.<prop>, which can be configured via the project properties ui.

If you have settings that are valid for more than one app (and your desired path implies this), my approach would be to introduce an individual configuration (which might also have an individual format) and read the properties manually.

You might provide the path to the config file as a default application setting with placeholders as you posted (e. g. [user], [company]).

NB: the last segment of the default user settings directory is the program version (assemby version or file version; not sure). If you change that, settings stored with an older version are not available until copied from the older version's user directory.

Jürgen Röhr
  • 886
  • 6
  • 10
  • When you say `You might provide the path to the config file as a default application setting with placeholders as you posted (e. g. [user], [company]).` - How would I do this, as this sounds like what I need ? – AxleWack Mar 24 '23 at 06:20
  • Just add a setting like `ConfigFilePath` with a value of `[user]\AppData\Local\[company]\user.config`. Use that to determine from where you're config must be read. But you have to do the reading and deserialization yourself. And you won't find those settings in the `.Properties.Settings.Default` class and they won't be readable via `ConfigurationManager`. – Jürgen Röhr Mar 24 '23 at 09:59
  • Thanks man, will try it out. If not, Ill try use chat gpt to help out, thanks that you at least tried to help, unlike those closing the question – AxleWack Mar 24 '23 at 22:22