我正在为我们构建的Web应用程序创建设置。不,我有一个类似于此的配置文件,其中包含“appSettings”部分和“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>
现在,从我的设置中,我必须从文件系统中打开配置文件 配置config = WebConfigurationManager.OpenWebConfiguration(“/”+ targetvdir); 变量'targetvdir'包含配置文件的路径。
我得到了配置文件,我可以通过
编辑'appSettings'部分config.AppSettings.Settings["Password"].Value = "something";
但是我无法用'applicationSettings'部分做到这一点。在Web应用程序本身中,我通过
访问该部分Properties.Settings.Default.<Setting>
但是我的设置项目无法正常工作。
是否有可能像“appSettings”部分一样轻松编辑'applicationSettings'部分?或者我是否必须编辑xml本身? 非常感谢任何提示。
亲切的问候, 凯哈特曼
答案 0 :(得分:1)
我为自己回答我的问题而道歉,因为我在发布后立即找到了解决方案。这个问题基本上给出了答案: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();