如何更新自定义配置部分中的 .NET 配置?

How do I update .NET configuration in a custom config section?

我们有一个应用程序有一个 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 元素?

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

// 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" );

我们可以创建自定义配置部分 class。

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>