c# 配置文件App.config操作类库的方法
更新时间:2016年12月02日 09:31:35 投稿:jingxian
下面小编就为大家带来一篇c# 配置文件App.config操作类库的方法。小编觉的挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实例如下:
public 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操作类库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
asp.net core 使用 tensorflowjs实现 face recognition的源代码
tensorflowjs,在该项目中使用了ml5js这个封装过的机器学习JavaScript类库, 使用起来更简单,本文给大家分享asp.net core 使用 tensorflowjs实现 face recognition的源代码,需要的朋友参考下吧2021-06-06
DataGridView不显示最下面的新行、判断新增行、删除行操作
这篇文章介绍了DataGridView不显示最下面的新行、判断新增行、删除行的操作方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02


最新评论