For my project I have settings that I added through the Settings in the project properties.
I quickly discovered that editing the app.config file directly seems to
I had the same problem until I realized I was running the app in debug mode, therefore my new appSetting's key was being written to the [applicationName].vshost.exe.config file.
And this vshost.exe.config file does NOT retain any new keys once the app is closed -- it reverts back to the [applicationName].exe.config file's contents.
I tested it outside of the debugger, and the various methods here and elsewhere to add a configuration appSetting key work fine. The new key is added to:[applicationName].exe.config.
Your answer is probably here: Simplest way to have a configuration file in a Windows Forms C# Application
Try this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!--- ->
</configSections>
<userSettings>
<Properties.Settings>
<setting name="MainFormSize" serializeAs="String">
<value>
1022, 732</value>
</setting>
<Properties.Settings>
</userSettings>
<appSettings>
<add key="TrilWareMode" value="-1" />
<add key="OptionsPortNumber" value="1107" />
</appSettings>
</configuration>
Reading values from App.Config File:
//This method will read the value of the OptionsPortNumber in the
//above app.config file.
private int LoadConfigData ()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
// AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
// points to the config file.
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
int smartRefreshPortNumber = 0;
foreach (XmlNode node in doc.ChildNodes.Item(1))
{
//Searching for the node “”
if (node.LocalName == "appSettings")
{
smartPortNumber =Convert.ToInt32(node.ChildNodes.Item(1).Attributes[1].Value);
}
}
Return smartPortNumber;
}
Updating the value in the App.config:
//This method will read the value of the OptionsPortNumber in the
//above app.config file.
private void UpdateConfigData()
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
//Looping through all nodes.
foreach (XmlNode node in doc.ChildNodes.Item(1))
{
//Searching for the node “”
if (node.LocalName == "appSettings")
{
if (!dynamicRefreshCheckBox.Checked)
{
node.ChildNodes.Item(1).Attributes[1].Value = this.portNumberTextBox.Text;
}
else
{
node.ChildNodes.Item(1).Attributes[1].Value = Convert.ToString(0);
}
}
}
//Saving the Updated values in App.config File.Here updating the config
//file in the same path.
doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
How are you referencing the Settings class in your code? Are you using the default instance or creating a new Settings object? I believe that the default instance uses the designer generated value which is re-read from the configuration file only when the properties are opened. If you create a new object, I believe that the value is read directly from the configuration file itself rather from the designer-generated attribute, unless the setting doesn't exist in the app.config file.
Typically my settings will be in a library rather than directly in the application. I set valid defaults in the properties file. I can then override these by adding the appropriate config section (retrieved and modified from the library app.config file) in the application's configuration (either web.config or app.config, as appropriate).
Using:
Settings configuration = new Settings();
string mySetting = configuration.MySetting;
instead of:
string mySetting = Settings.Default.MySetting;
is the key for me.
This is silly ... and I think I am going to have to apologize for wasting everyone's time! But it looks like I just need to set the scope to User instead of Application and I can the write the new value.
System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["oldPlace"].Value = "3";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Try with this code , is easy.
Atte: Erick Siliezar