【问题标题】:How do I update .NET configuration in a custom config section?如何在自定义配置部分更新 .NET 配置?
【发布时间】:2016-03-22 04:46:29
【问题描述】:

我们有一个包含 exe.config 文件的应用程序,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <configSections>
        <sectionGroup name="ConsoleSettings">
        <section name="Console" type="MyApp.ConfigSection" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>    
    </configSections>

    <ConsoleSettings>
        <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" />
    </ConsoleSettings>
....

我想做的是读取文件,将 LanAddress 更改为用户输入的内容(例如,string newLanAddress),然后将其保存回来。

到目前为止,我有这个:

var configFile = new ExeConfigurationFileMap();
var configFile.ExeConfigFilename = "MyApp.exe.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);

var configGroup = config.SectionGroups[@"ConsoleSettings"];
var consoleSection = configGroup.Sections[0];
var lanAddress = consoleSection.// this is where I get stuck

如何访问 consoleSection 的 LanAddress 元素??

【问题讨论】:

  • 你不能用System.Configuration.ConfigurationManager.AppSettings[key];获得价值

标签: c# configurationmanager


【解决方案1】:

我们可以创建自定义配置节类。

public class ConsoleSection : ConfigurationSection
{
    [ConfigurationProperty("Username", IsRequired = true)]
    public string Username
    {
        get
        {
            return (string)this["Username"];
        }
        set
        {
            this["Username"] = value;
        }
    }

    [ConfigurationProperty("Password", IsRequired = true)]
    public String Password
    {
        get
        {
            return (String)this["Password"];
        }
        set
        {
            this["Password"] = value;
        }
    }

    [ConfigurationProperty("LanAddress", IsRequired = true)]
    public string LanAddress
    {
        get
        {
            return (string)this["LanAddress"];
        }
        set
        {
            this["LanAddress"] = value;
        }
    }

    [ConfigurationProperty("Port", IsRequired = false)]
    [IntegerValidator(ExcludeRange = false, MaxValue = short.MaxValue, MinValue = short.MinValue)]
    public int Port
    {
        get
        {
            return (int)this["Port"];
        }
        set
        {
            this["Port"] = value;
        }
    }
}

要阅读配置部分,我们应该执行以下操作。

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var consoleSection = (ConsoleSection)config.GetSection("ConsoleSettings/Console");
System.Console.WriteLine("ip: {0}", consoleSection.LanAddress);

App.config 与您的非常相似。

<configSections>
  <sectionGroup name="ConsoleSettings">
    <section name="Console" type="MyApp.ConsoleSection, MyApp" allowLocation="true" allowDefinition="Everywhere" />
  </sectionGroup>
</configSections>
<ConsoleSettings>
  <Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" />
</ConsoleSettings>

【讨论】:

    【解决方案2】:

    这将打开默认的应用程序配置文件。它会更改连接字符串部分,但您应该能够修改它以更新您的自定义部分。

    // get the config file for this application
    Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None );
    
    // set the new values
    config.ConnectionStrings.ConnectionStrings["Connection Name"].ConnectionString = "Connection String Value";
    
    // save and refresh the config file
    config.Save( ConfigurationSaveMode.Minimal );
    ConfigurationManager.RefreshSection( "connectionStrings" );
    

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2010-10-25
      • 2012-03-30
      • 2010-11-12
      • 2013-09-17
      • 2011-07-04
      • 2012-12-12
      • 2014-02-14
      相关资源
      最近更新 更多