C# Winform 配置文件App.config

目录

一、简介

二、添加引用 

三、添加节点

1.普通配置节点

2.数据源配置节点

四、管理类 ConfigHelper.cs

1.获取配置节点

2.更新或加入配置节点

结束


一、简介

在C#中,配置文件很常用,ASP.NET 和 Winform 名称不同,用法一样,如下图

config 文件通常用来存储一些需要修改的数据,比如用户名密码,连接数据库的IP地址等,而不是在代码中写死。有人可能会问,那我自己自定义一个配置文件也行,为什么要用它这个?区别当然有,微软自己封装的读取和写入会更简单一些,你自己封装的,就要自己去封装测试,但最终的效果其实是一样的。

二、添加引用 

添加 System.Configuration.dll

三、添加节点

常用的下面两种,第一种是常用的方式 appSettings,第二种是用在数据库的 connectionStrings

1.普通配置节点

<appSettings>  
  <add key="COM1" value="我是一个串口号" />
</appSettings> 

这种写法,读取方式可以用下面的方式

string connStr = ConfigurationManager.AppSettings["url"];

如果是 int 类型,可以直接转换类型

<add key="app.year" value="2018"/>
<add key="app.month" value="11"/>
<add key="app.day" value="08"/>

int  appYear = int.Parse(ConfigurationManager.AppSettings["app.year"]);
int  appMonth = int.Parse(ConfigurationManager.AppSettings["app.month"]);
int  appDay = int.Parse(ConfigurationManager.AppSettings["app.day"]);

2.数据源配置节点

<connectionStrings>
  <add name="kyd" connectionString="server=.;database=xxxxx;user=sa;pwd=123"/>
</connectionStrings>

四、管理类 ConfigHelper.cs

代码:

using System.Configuration;

namespace Utils
{
    public class ConfigHelper
    {
        ///<summary> 
        ///返回*.exe.config文件中appSettings配置节的value项 
        ///</summary> 
        ///<param name="strKey"></param> 
        ///<returns></returns> 
        public static string GetAppConfig(string strKey)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == strKey)
                {
                    return config.AppSettings.Settings[strKey].Value.ToString();
                }
            }
            return null;
        }

        ///<summary> 
        ///在*.exe.config文件中appSettings配置节增加一对键值对 
        ///</summary> 
        ///<param name="newKey"></param> 
        ///<param name="newValue"></param> 
        public static void UpdateAppConfig(string newKey, string newValue)
        {
            string file = System.Windows.Forms.Application.ExecutablePath;
            Configuration config = ConfigurationManager.OpenExeConfiguration(file);
            bool exist = false;
            foreach (string key in config.AppSettings.Settings.AllKeys)
            {
                if (key == newKey)
                {
                    exist = true;
                }
            }
            if (exist)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            config.AppSettings.Settings.Add(newKey, newValue);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

    }
}

1.获取配置节点

//获取配置节
string res = ConfigHelper.GetAppConfig("COM1");
Console.WriteLine("====================" + res);

效果:

2.更新或加入配置节点

//更新或加入配置节
ConfigHelper.UpdateAppConfig("COM2", "xxxxxxxx");

运行后,我们需要在配置文件中查看效果是否实现,打开生成目录下的config文件

可以看到,已经自动加入了一个配置节

结束

如果这个帖子对你有用,欢迎关注 + 点赞 + 留言,谢谢

end

  • 14
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊思宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值