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

时间:2016-03-21 19:25:36

标签: c# configurationmanager

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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非常类似于你的。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;