【问题标题】:How to modify/update custom config section of app.config using c#如何使用 c# 修改/更新 app.config 的自定义配置部分
【发布时间】:2015-08-11 06:11:51
【问题描述】:

我正在尝试在 app.config 中为我的 Windows 应用程序保存一些配置。 我在 Google 中搜索了解决方案,但没有与此相关的任何内容。我的要求是我想使用我的 Windows 应用程序保存/更新 app.config 中的配置。

我的架构将是这样的:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="customAppSettings1" type="System.Configuration.NameValueSectionHandler"/>
    <section name="customAppSettings2" type="System.Configuration.NameValueSectionHandler"/>    
  </configSections>
  <customAppSettings1>
    <add key="FirstKey" value="1" />
    <add key="SecondKey" value="2" />
  </customAppSettings1>
  <customAppSettings2>
    <add key="FirstKey" value="1" />
    <add key="SecondKey" value="2" />
  </customAppSettings2>
</configuration>

【问题讨论】:

标签: c# app-config


【解决方案1】:

此代码提供更改的自定义值。但不保存在物理文件上。你的问题有点进步。

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
xmlDoc.SelectSingleNode("//applicationSettings/MyApp_Application.Properties.Settings/setting/value").InnerText = "server=127.0.0.1;uid=root;pwd=root;database=" + comboBox1.SelectedItem.ToString();

xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("applicationSettings/MyApp_Application.Properties.Settings/setting");

【讨论】:

    猜你喜欢
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2016-05-02
    • 2015-07-05
    • 2011-05-25
    • 2012-01-28
    • 2012-10-28
    相关资源
    最近更新 更多