从设置访问配置文件中的“applicationSettings"(不是“appSettings")部分 [英] Access section 'applicationSettings' (not 'appSettings') in config file from setup
问题描述
我正在为我们构建的 Web 应用程序创建设置.不,我有一个配置文件,它看起来像这样,其中包含一个appSettings"部分和一个applicationSettings"部分:
<预><代码><配置><应用设置><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><应用程序设置><inoBIBooks.My.MySettings><setting name="BIDB_Username" serializeAs="String"><value>用户名</value></设置><setting name="BIDB_Server" serializeAs="String"><value>服务器名称</value></设置><setting name="BIDB_Database" serializeAs="String"><value>数据库</value></设置><setting name="BIDB_Password" serializeAs="String"><value>密码</value></设置></inoBIBooks.My.MySettings></applicationSettings></配置>现在,从我的设置中,我必须从文件系统中打开配置文件配置 config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);其中变量targetvdir"包含配置文件的路径.
我由此获得了配置文件,并且可以通过
编辑appSettings"部分config.AppSettings.Settings["Password"].Value = "something";
但无论如何我都无法使用applicationSettings"部分来做到这一点.在 Web 应用程序本身中,我通过
访问该部分Properties.Settings.Default.
但这在我的设置项目中不起作用.
是否有机会像编辑appSettings"部分一样简单地编辑applicationSettings"部分?还是我必须编辑 xml 本身?非常感谢任何提示.
亲切的问候,凯·哈特曼
很抱歉自己回答了我的问题,因为我在发布后立即找到了解决方案.这个问题基本上给出了答案:Save and reload app.config(applicationSettings) at runtime
我必须使用这段代码来写入applicationSettings"部分:
//这将获取 applicationSettings 部分(以及内部部分 'inoBIBooks.My.MySettings')配置 config = WebConfigurationManager.OpenWebConfiguration("/" + targetvdir);ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["inoBIBooks.My.MySettings"];ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;//为该特定属性设置一个值SettingElement applicationSetting = clientSection.Settings.Get("BIDB_Username");applicationSetting.Value.ValueXml.InnerText = "用户名";//没有这个,保存将不起作用applicationConfigSection.SectionInformation.ForceSave = true;//节省config.Save();
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
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();
这篇关于从设置访问配置文件中的“applicationSettings"(不是“appSettings")部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!