简体   繁体   English

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

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

We have an application that has a exe.config file much like this: 我们有一个具有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>
....

What I would like to do is read the file, change the LanAddress to something the user entered (say, string newLanAddress ) and then save it back. 我想做的是读取文件,将LanAddress更改为用户输入的内容(例如, string newLanAddress ),然后将其保存回去。

So far, I have this: 到目前为止,我有这个:

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

How do I access the LanAddress element of consoleSection?? 如何访问consoleSection的LanAddress元素?

This opens the default application config file. 这将打开默认的应用程序配置文件。 It changes a connection string section but you should be able to modify it to update your custom section. 它更改了连接字符串部分,但是您应该可以对其进行修改以更新您的自定义部分。

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

We can create custom configuration section 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;
        }
    }
}

To read the config section we should do the following. 要阅读config部分,我们应该执行以下操作。

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

App.config is very similar to your one. 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM