c# 配置文件App.config操作类库的方法
时间:2021-03-14 09:50:29|栏目:.NET代码|点击: 次
实例如下:
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
上一篇:汉字转拼音缩写示例代码(Silverlight和.NET 将汉字转换成为拼音)
栏 目:.NET代码
下一篇:ASP.NET中使用开源组件NPOI快速导入导出Execl数据
本文地址:http://www.codeinn.net/misctech/80554.html






