-2

The user.config file of an application can be accessed in C# by ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath

Now, I need to get the user.config of a DIFFERNT application, identified by the fullpath to its exe in order replace current applications user.config by that one.

Any suggestions how to do so?

Edit: Please notice that I am interested in the user.config (ConfigurationUserLevel.PerUserRoamingAndLocal), not the application level settings like app.config or Application.exe.config. The latter is accessable by the OpenExeConfiguration(string), but is not what I want.

curator
  • 175
  • 2
  • 10
  • E.g. this: http://stackoverflow.com/questions/22785541/accessing-another-projects-app-config-properties – MakePeaceGreatAgain Apr 19 '17 at 13:02
  • Possible duplicate of [Accessing another projects app.config properties?](http://stackoverflow.com/questions/22785541/accessing-another-projects-app-config-properties) – Foxan Ng Apr 19 '17 at 13:10
  • I DID google this, but didn't find a solution. And it is no duplicate, since I am not interested in app level config, but user level config – curator Apr 19 '17 at 13:20

1 Answers1

1

What about the overload that accepts a string?

var config = ConfigurationManager.OpenExeConfiguration(pathToAssebmly);

Anyway it´s a bad idea to bind your assembly that way to another one. You should consider to copy the config-file into all your assemblies and change the parts that are specific to a given one.

EDIT: As you´re interested in the user-specific setting you may use the following which is taken from here:

string configFile =  string.Concat(appName, ".config");  
// Map the new configuration file.
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap 
{ 
    ExeConfigFilename = configFile 
};

var config = ConfigurationManager.OpenMappedExeConfiguration(
        configFileMap, 
        ConfigurationUserLevel.PerUserRoamingAndLocal);
dotNET
  • 33,414
  • 24
  • 162
  • 251
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Thanks, that totally leads to the correct solution. However, the Property LocalUserConfigFileName of the configFileMap is string.Empty, which leads to an Execption – curator Apr 19 '17 at 14:00
  • Then I suppose you have to set this property instead of the `ExeConfigFilename` t the location of your config-file. – MakePeaceGreatAgain Apr 19 '17 at 14:07
  • Which is exactly the problem, since this path is computed in a way that I cannot reproduce, see [link](http://stackoverflow.com/questions/20120457/define-a-custom-path-where-the-user-config-file-should-be-saved). I thought that this can be done automatically without reverse engeneering – curator Apr 19 '17 at 14:15