Access section 'applicationSettings' (not

2019-05-28 22:55发布

问题:

I am in the process of creating a setup for a web application we build. No I have a configuration file, which looks something like like this, which contains a section 'appSettings', and a section 'applicationSettings':

<configuration>
<appSettings>
    <add key="Password" value="dummy"/>
    <add key="Username" value="dummy"/>
    <add key="DB" value="dummy"/>
    <add key="DBServer" value="dummy"/>
    <add key="LogStoredProcedure" value="dummy"/>
    <add key="ErrorStoredProcedure" value="dummy"/>
    <add key="ErrorFileName" value="dummy"/>
    <add key="EncryptionKey" value="dummy"/>
</appSettings>
<applicationSettings>
    <inoBIBooks.My.MySettings>
      <setting name="BIDB_Username" serializeAs="String">
        <value>Username</value>
      </setting>
      <setting name="BIDB_Server" serializeAs="String">
        <value>Servername</value>
      </setting>
      <setting name="BIDB_Database" serializeAs="String">
        <value>Database</value>
      </setting>
      <setting name="BIDB_Password" serializeAs="String">
        <value>Password</value>
      </setting>
    </inoBIBooks.My.MySettings>
</applicationSettings>
</configuration>

Now, from my setup I have to open the config file from the file system with Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir); where variable 'targetvdir' contains the path to the config file.

I get the config file by this, and I am able to edit the 'appSettings' section by

config.AppSettings.Settings["Password"].Value = "something";

But I am not able to do that anyway with the 'applicationSettings' section. In the web application itself I access that part by

Properties.Settings.Default.<Setting>

but that wont work from my setup project.

Is there a chance to edit the 'applicationSettings' section as easy as the 'appSettings' section? Or do I have to edit the xml itself? Any hint is much appreciated.

Kind regards, Kai Hartmann

回答1:

I apologize for answering my question by myself, since I found the solution right after posting it. This question gave the answer basically: Save and reload app.config(applicationSettings) at runtime

I had to use this code, to write to that section 'applicationSettings':

// this gets the applicationSettings section (and the inner section 'inoBIBooks.My.MySettings')
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

// set a value to that specific property
SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");
applicationSetting.Value.ValueXml.InnerText = "username";

// without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = true;
// save
config.Save();