I have a WinForms application that is deployed using the Visual Studio 2008 publishing system (ClickOnce). In the application file app.config, I have a configuration section that is required by a third-party component that has the form:
<section name="thirdPartySection"
type="System.Configuration.NameValueSectionHandler" />
So the section is not in appSettings and looks like this:
<thirdPartySection >
<add key="someKey" value="someValue" />
</thirdPartySection >
I understand that key / value pairs are NameValueCollection. The problem I am facing is that I want to change the value of both deployment time and runtime (or it’s good with me), so it someValuewill be someOtherValuebased on the installed environment.
I am currently making some other configuration changes at runtime, but they are in the section AppSettingsand therefore easy to get. I found many links in my search for a solution, but they seem to rely on a section with a custom class, and not with the name NameValueCollection that I come across.
Does anyone know a better way to change this data? Changing the runtime using ConfigurationManager.RefreshSection () will be more consistent with my current code, but I am also open to suggestions during the installation phase.
Edit: this works at runtime. This is how I handled the old configuration overrides.
Configuration config = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
config.AppSettings.Settings["Main.ConnectionString"].Value =
PolicyTrackerInfo.ConnectionString;
config.AppSettings.Settings["Main.linq"].Value =
PolicyTrackerInfo.LinqConnectionString;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
My attempt to do the same for another section:
string overwriteXml = config.GetSection("thirdPartySection")
.SectionInformation.GetRawXml();
XmlDocument xml = new XmlDocument();
xml.LoadXml(overwriteXml);
XmlNode node = xml.SelectSingleNode("thirdPartySection/add");
node.Attributes["value"].Value = PolicyTrackerInfo.OverwriteString;
. , XML . ?
: app.config.deploy. , , . , .