街角_祝福

导航

C#程序配置使用示例

通用帮助类

/// <summary>
/// 配置帮助类
/// </summary>
public class ConfigHelper
{
    public static Configuration GetConfig()
    {
        return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    }

    public static T GetSection<T>(string name=null) where T : class
    {
        if(name == null)
        {
            name = typeof(T).Name;
        }
        return GetConfig().GetSection(name) as T;
    }
}

自定义配置AppSettings

  • 配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="F1" value="v1"/>
        <add key="F2" value="v1"/>
        <add key="F3" value="v3"/>
    </appSettings>
</configuration>
  • 代码引用
var appSettings = ConfigurationManager.AppSettings;
var f1 = appSettings.Get("F1");
var f2 = appSettings.Get("F2");
var f3 = appSettings.Get("F3");
Log($"F1:{f1} F2:{f2} F3:{f3}");

自定义配置组(键值对:NameValueSectionHandler)

  • 配置文件
<!--App.config-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <!--自定义键值对配置项-->
        <section name="NameValueNode" type="System.Configuration.NameValueSectionHandler"/>
    </configSections>
    <!--配置内容为指定的外部文件-->
    <NameValueNode configSource="NameValueNode.xml" />
</configuration>

<!--NameValueNode.xml-->
<?xml version="1.0" encoding="utf-8" ?>
<NameValueNode>
    <add key="键值1" value="数值1" />
    <add key="键值2" value="数值2" />
    <add key="键值3" value="数值3" />
</NameValueNode>
  • 代码引用
var nameValueNode = (System.Collections.Specialized.NameValueCollection)ConfigurationManager.GetSection("NameValueNode");
foreach(var v in nameValueNode.AllKeys)
{
    Log($"{v}:{nameValueNode[v]}");
}

自定义配置单项(多属性值:SingleTagSectionHandler)

  • 配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <!--自定义属性配置单项-->
        <section name="SingleTagNode" type="System.Configuration.SingleTagSectionHandler"/>
    </configSections>
    <!--自定义配置详情-->
    <SingleTagNode F1="公平" F2="公正" F3="富强" F4="民主"/>
</configuration>
  • 代码引用
var singleTagNode = (System.Collections.Hashtable)ConfigurationManager.GetSection("SingleTagNode");
foreach (var v in singleTagNode.Keys)
{
    Log($"{v}:{singleTagNode[v]}");
}

自定义对象单项配置

  • 配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <!-- 单节点,name为标签名,type为类型,格式:域名.类名,域名,其中类名的父类为ConfigurationSection -->
        <section name="MySingle" type="AppConfigDemo.MySingleConfig, AppConfigDemo"/>
    </configSections>
    <!--自定义配置详情-->
    <MySingle F1="美利坚" F2="2023" F3="年死掉了"/>
</configuration>
  • 代码实现
namespace AppConfigDemo
{
    public class MySingleConfig : ConfigurationSection
    {
        [ConfigurationProperty("F1")]
        public string F1
        {
            get => (string)this["F1"];
            set => this["F1"] = value;
        }
        [ConfigurationProperty("F2")]
        public int F2
        {
            get => (int)this["F2"];
            set => this["F2"] = value;
        }
        [ConfigurationProperty("F3")]
        public string F3
        {
            get => (string)this["F3"];
            set => this["F3"] = value;
        }

        #region 其他
        public static MySingleConfig Get()
        {
            return ConfigHelper.GetSection<MySingleConfig>("MySingle");
        }
        #endregion
    }
}
  • 配置引用
// 读取配置项到对象
var singleConfig = MySingleConfig.Get();

自定义对象多项(数组或集合)配置

  • 配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <!-- 多节点,name为标签名,type为类型,格式:域名.类名,域名,其中类名的父类为ConfigurationSection -->
        <section name="MyMultiConfig" type="AppConfigDemo.MyMultiConfig, AppConfigDemo"/>
    </configSections>
    <!--自定义配置详情-->
    <MyMultiConfig>
        <Items>
            <add F1="张晓敏" F2="20230310" F3="早上起晚了"/>
            <add F1="何曼丽" F2="20490605" F3="午睡摔倒了"/>
            <add F1="李张绍" F2="30250608" F3="晚上吃多了"/>
        </Items>
    </MyMultiConfig>
</configuration>
  • 代码实现
public class MySingleConfigCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MySingleConfig();
    }

    protected override object GetElementKey(ConfigurationElement e)
    {
        return (e as MySingleConfig).F1;
    }
}


public class MyMultiConfig : ConfigurationSection
{
    [ConfigurationProperty("Items", IsDefaultCollection = false)]
    public MySingleConfigCollection ConfigCollection
    {
        get { return (MySingleConfigCollection)this["Items"]; }
        set { this["Items"] = value; }
    }
}
  • 配置引用
var multiConfig = ConfigHelper.GetSection<MyMultiConfig>();
foreach(MySingleConfig v in multiConfig.ConfigCollection)
{
    
}

posted on 2023-03-10 14:49  街角_祝福  阅读(22)  评论(0编辑  收藏  举报