Reputation: 2533
I have been researching this issue for long. But I haven't come across any satisfying solution.
The scenario is that I have a WPF client application. I have a couple of web references added to the project and I Settings.Designer.cs file was modified and had a hard coded reference to the srever url and port. This started getting reflected in my app.config file in ApplicationSettings section.
Before the user logs in, he can specify the settings for the ServerIP and Port for the server. Now I would like to save these settings in app.config and get the value for server IP and port picked up from there or save it elsewhere and connect to the server through this IP and Port. I would like these changes to persist.
One solution that I could think of was to read the app.config through an XML reader, modify the file, save it and restart the application somehow.
I am not able to think of a better scenario as I think ApplicationSettings section can not be modified from inside the application.
EDIT:
My applicationSettiings section looks something like this:
<applicationSettings>
<ApplicationName.Properties.Settings>
<setting name="Web_Service_Reference_Name1" serializeAs="String">
<value>http://10.1.100.118:8080/AD/WebService1</value>
</setting>
<setting name="Web_Service_Reference_Name2" serializeAs="String">
<value>http://10.1.100.118:8080/AD/WebService2</value>
</setting>
</ApplicationName.Properties.Settings>
</applicationSettings>
Upvotes: 0
Views: 1575
Reputation: 216243
Sometimes ago a similar question was posted on this site.
I have a simple solutions that looks like this:
public void WriteLocalValue(string localKey, string curValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
KeyValueConfigurationElement k = config.AppSettings.Settings[localKey];
if (k == null)
config.AppSettings.Settings.Add(localKey, curValue);
else
k.Value = curValue;
config.Save();
}
public string ReadLocalValue(string localKey, string defValue)
{
string v = defValue;
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration( Application.ExecutablePath);
KeyValueConfigurationElement k = config.AppSettings.Settings[localKey];
if (k != null) v = (k.Value == null ? defValue : k.Value);
return v;
}
catch { return defValue; }
}
Upvotes: 1