I’m using app.config file to store and read some parameters (sql server instance name, user, password, log directory, etc). Now, I need to modify some parameters which depends of user and managed this but only if I run .exe from bin/release directory.
When I create setup and install my aplication I’m not able to change this parameters – it throws TargetInvocationException. I’ve tried to run my app as administrator but without success.
The code which I currently use is the following:
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("username");
config.AppSettings.Settings.Add("username", this.Config.Username);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
I’ve tried a few other solutions found on stackoverflow but without success.
Ideally we cannot modify config entries when application is running.
When you ran the exe from bin, it didn’t modify the *.exe.config .
instead it modified *.vshost.exe.Config file.
*.exe.config is read only, you cannot update this file.