65.9K
CodeProject 正在变化。 阅读更多。
Home

通用配置节读取

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.62/5 (4投票s)

2008 年 3 月 5 日

CPOL

1分钟阅读

viewsIcon

24398

downloadIcon

161

配置节的泛型读取。

目录

引言

获取和存储设置是编程中一项必要的技能。 如果你需要以泛型和类型安全的方式存储和检索配置节,那么这篇文章将对你有所帮助。

演示应用程序是一个 Windows socket 客户端,我用它来测试 socket 协议。

常用设置

存储和检索设置的常用方法是使用 AppSettingsConnectionString,这些方法用于简单的配置。

当你需要从配置文件创建对象时,创建节是最佳途径。 将要使用配置节的类将从 ConfigurationSection 类派生。

ConfigurationSection

ConfigurationSection 派生是一种简单而强大的方法,可以以面向对象的方式使用配置节。

应用程序配置将存储数据并将它们链接到其类型。

class OptionsConfigHandler : ConfigurationSection
{
    [ConfigurationProperty("HostName", IsRequired = true, 
      IsKey = false, DefaultValue = "127.0.0.1")]
    public string HostName
    {
        get { return (string)base["HostName"]; }
        set { base["HostName"] = value; }
    }

    [ConfigurationProperty("Port", IsRequired = true, 
      IsKey = false, DefaultValue = "4433")]
    public string Port
    {
        get { return (string)base["Port"]; }
        set { base["Port"] = value; }
    }
}

OptionsConfigHandler 类可用于从应用程序配置文件中的节检索数据。 当此类用于将数据存储到应用程序设置时,设置将如下所示

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="OptionSection" 
          type="SocketTest.OptionsConfigHandler, SocketTest, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" allowLocation="true"
allowDefinition="Everywhere"
allowExeDefinition="MachineToApplication"
overrideModeDefault="Allow"
restartOnExternalChanges="true" requirePermission="true" />
    </configSections>
    <OptionSection HostName="127.0.0.1" Port="4433" />
</configuration>

public class ExeConfig
{
    private static Configuration config;
    public static Configuration Config
    {
        get
        {
            if (config == null)
                config = ConfigurationManager.OpenExeConfiguration(
                                      ConfigurationUserLevel.None);
            return config;
        }
    }

    public static T GetSection<T>(string sectionName) where T : 
                                                  ConfigurationSection
    {
        T options = null;
        try
        {
            options = Config.GetSection(sectionName) as T;
        }
        catch
        {
            // Ignore, it is up to the user to check for null values
        }

        return options;
    }

    public static void AddSection(string name, ConfigurationSection section)
    {
        config.Sections.Add(name, section);
        Config.Save(ConfigurationSaveMode.Full);
    }

    public static void Save()
    {
        Config.Save(ConfigurationSaveMode.Modified);
    }
}

这个类读取和写入配置文件的节,你只需要它就可以开始。

读取节

要从配置文件读取 OptionsConfigHandler 节,这是代码

OptionsConfigHandler options = ExeConfig.GetSection<OptionsConfigHandler>(sectionName);

写入节

要将 OptionsConfigHandler 节写入配置文件,这是代码

OptionsConfigHandler options = 
     ExeConfig.GetSection<OptionsConfigHandler>(sectionName);
if (options == null)
{
    options = new OptionsConfigHandler();
    ExeConfig.AddSection(sectionName, options);
}
ExeConfig.Save();

完成了!

修订历史

© . All rights reserved.