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.
c:\program files
are not writable. You'll need a separate program that edits the file so it can ask for UAC elevation. Or not use settings to store this info, an .xml file in AppData works too. – Hans Passant Jan 10 '12 at 18:42