Chase
Chase

Reputation: 564

Wrong app.config loading

I have a simple C# console application that needs to pull a few values out of the app.config file. The problem is it seems to be opening a separate, hidden, config file that I can't find or figure out how to fix.

Here is what I'm doing, with some extra crap added and names changed as I've been using trial and error up to this point:

var configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

var fileMap = new ExeConfigurationFileMap();

fileMap.RoamingUserConfigFilename = configPath;
fileMap.ExeConfigFilename = configPath;
fileMap.LocalUserConfigFilename = configPath;

var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

ConfigurationSection section = config.GetSection("appSettings");

section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;

section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
string value = config.AppSettings.Settings["ValueHere"].Value;
string otherValue = ConfigurationManager.AppSettings["ValueHere"].ToString();

The configPath points to the correct path where the files including the app.config are located. The name of the app.config is APPLICATION_NAME.exe.config.

If I delete the config file the application will happily return values so I'm 100% certain it's referencing some other file I just cannot figure out where that file is or how to fix it.

Running the application in debugging points to the correct path but of course that's located in the bin -> debug folder and of little use. Only in install testing and production does this issue occur.

Edit: There is only ONE app.config file in the folder contents. The application is searching and finding this other one elsewhere.

Upvotes: 0

Views: 1879

Answers (1)

Emond
Emond

Reputation: 50672

If a setting has a user scope it will be written to a config file in the user's (roaming) profile. See: When using a Settings.settings file in .NET, where is the config actually stored?

So check the scope of the setting's properties.

Upvotes: 1

Related Questions