Changing configuration sections in App.config either at run time or during installation

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. , , . , .

+3
3

, , , , , . , , , . , :

<thirdPartySection>
    <add key="someKey" value="#NEEDS_INITIALIZED#" />
</thirdPartySection >

Main :

static public void Main(params string[] args)
{
    const string uninitializedValue = "#NEEDS_INITIALIZED#";

    // Load the third-party config section (this assumes it inherits from
    // ConfigurationElementCollection
    var config = ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.None);
    var section = config.GetSection("thirdPartySection") 
        as NameValueConfigurationCollection;
    var setting = section["someKey"];
    if (setting.Value == uninitializedValue)
    {
        setting.Value = PolicyTrackerInfo.OverwriteString;
        config.Save();
    }
}
+1

, ( , , ), , : http://www.devx.com/dotnet/Article/10045

, ClickOnce shim, XCOPY ( app.config, XML- ).

, , , . ?

+1

, AfterInstall XML, . , ClickOnce.

0

Source: https://habr.com/ru/post/1729482/


All Articles