背景需求
在C#应用程序中经常会把一些配置信息、系统参数信息放到配置文件中,而最常用的配置文件就是应用程序配置文件App.Config或Web.Config。是一个基于XML格式的文档。本文主要说一下关于配置文件中的appSettings配置节内容的读写实现问题。关于appSettings内容的读取我相信大家都很熟性,就是使用
System.Configuration.ConfigurationManager.AppSettings[appSettingKey];
当然要在工程中添加对System.Configuration的引用。
对与写入操作很多朋友都没有用到过, 起始微软已在.netframework中提供了对于应用程序配置文件的写入支持。
实现代码
可以把对App.Config文件的读写操作封装到一个类AppConfigHelper中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Wongoing.Basic
{
/// <summary>
/// 应用程序配置文件访问辅助类
/// </summary>
public class AppConfigHelper
{
/// <summary>
/// 读取string配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的string类型value值</returns>
public static string GetAppSettingValue(string appSettingKey, string defaultValue)
{
string result = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(result))
{
return defaultValue;
}
return result;
}
/// <summary>
/// 读取bool配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的bool类型value值</returns>
public static bool GetAppSettingValue(string appSettingKey, bool defaultValue)
{
string result = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(result))
{
return defaultValue;
}
bool boolResult = false;
if (bool.TryParse(result, out boolResult))
{
return boolResult;
}
else
{
return defaultValue;
}
}
/// <summary>
/// 读取int配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的int类型value值</returns>
public static int GetAppSettingValue(string appSettingKey, int defaultValue)
{
string result = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(result))
{
return defaultValue;
}
int intResult = 0;
if (int.TryParse(result, out intResult))
{
return intResult;
}
else
{
return defaultValue;
}
}
/// <summary>
/// 读取double类型配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的double类型value值</returns>
public static double GetAppSettingValue(string appSettingKey, double defaultValue)
{
string result = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(result))
{
return defaultValue;
}
double doubleResult = 0.0;
if (double.TryParse(result, out doubleResult))
{
return doubleResult;
}
else
{
return defaultValue;
}
}
/// <summary>
/// 修改App.config中AppSetttings中的配置项
/// </summary>
/// <param name="name">要修改的配置项的名称</param>
/// <param name="value">要修改的配置项的值</param>
/// <returns></returns>
public static bool SetAppSettings(string name, string value)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//当前应用程序的配置文件
if (config != null)
{
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
if (appSettings.Settings.AllKeys.Contains(name))
{
appSettings.Settings[name].Value = value;
}
else
{
appSettings.Settings.Add(name, value);
}
config.Save(); //保存配置文件
return true;
}
return false;
}
catch(Exception ex)
{
Console.WriteLine(String.Format("修改app.config配置{0}的值为{1}异常:{2}", name, value, ex.Message));
return false;
}
}
}
}
这样通过调用AppConfigHelper.GetAppSettingValue()方法就可以读取相应配置项的值,通过调用AppConfigHelper.SetAppSettings()方法就可以写入相应配置项的值了。