WinForm AppConfig 的应用

       Winform的AppConfig支持自己定义节点,然后来维护一些数据。用来做配置文件一般来说基本够用了:

<configuration> 
  <configSections>
    <section name="User" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>
  <User>
    <add key="Ming" value="2"/>
  </User>

 下面是一个读取AppConfig的一些方法:

 

public static List<string> GetConfigList(string sectionname)// 把所有的Key作为List<string >返回
        {
            NameValueCollection Section = LoadConfig.loadConfig(sectionname);
            List<string> Items = new List<string>(); ;
            if (Section!= null)
            {
                for (int k = 0; k < Section.Keys.Count; k++)
                {
                    var item = Section.Keys[k];
                    Items.Add(Section.Keys[k]);
                }
            }
            return Items;
        }

        public static string GetConfigFirstValue(string sectionname)//读取只有一条的配置
        {
            NameValueCollection Section = LoadConfig.loadConfig(sectionname);
            List<string> Items = new List<string>(); ;
            if (Section != null)
            {   
                return Section.Keys[0].ToString();         
            }
            else
            {
                return null;
            }
            
        }

        public static DataTable GetConfigDT(string sectionname)//将配置数据返回为一个DataTable
        {
            NameValueCollection Section = LoadConfig.loadConfig(sectionname);
            DataTable Items=new DataTable() ;
            if (Section != null)
            {
                Items.Columns.Add("Key");
                Items.Columns.Add("Value");
                for (int k = 0; k < Section.Keys.Count; k++)
                {
                    Items.Rows.Add(Section.Keys[k].ToString(),Section.GetValues(k).GetValue(0).ToString());
                }
            }
            return Items;
        }
        public static string GetValuebyKey(string sectionname,string key)// 根据Key值得到Value值
        {

            NameValueCollection Section = LoadConfig.loadConfig(sectionname);
            if (Section != null)
            {
                for (int k = 0; k < Section.Keys.Count; k++)
                {
                    if (Section.Keys[k].ToString() == key)
                    {
                        return Section.GetValues(k).GetValue(0).ToString();
                    }
                }
            }
            return null;
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值