当前位置:首页 > 通信资讯 > 正文

实例如下:

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950public class ConfigOperator{#region 从配置文件获取Value/// <summary>/// 从配置文件获取Value/// </summary>/// <param name="key">配置文件中key字符串</param>/// <returns></returns>public static string GetValueFromConfig(string key){try{Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//获取AppSettings的节点 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");return appsection.Settings[key].Value;}catch{return "";}}#endregion#region 设置配置文件/// <summary>/// 设置配置文件/// </summary>/// <param name="key">配置文件中key字符串</param>/// <param name="value">配置文件中value字符串</param>/// <returns></returns>public static bool SetValueFromConfig(string key, string value){try{//打开配置文件 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//获取AppSettings的节点 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");appsection.Settings[key].Value = value;config.Save();return true;}catch{return false;}}#endregion

以上这篇c# 配置文件App.config操作类库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。