3

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

4

1 回答 1

2

我很抱歉自己回答我的问题,因为我在发布后立即找到了解决方案。这个问题基本上给出了答案:Save and reload app.config(applicationSettings) at runtime

我不得不使用这段代码来写入“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();
于 2013-07-01T11:27:54.610 回答