作为C#参数配置读取,必不可少的就需要用到System.Configuration类库,之前经常手动配置App.Config进行参数配置,然后使用
ConfigurationManager.AppSettings[appSettingKey];
来进行参数配置读取,其实这个类和INI文件类似,也是可以进行创建的,写入初始值的,AppConfigHelper见下面代码,注意:编译完成后的.Config文件名称需要跟可执行程序名称一致,否者读取不到对应的参数值
/// <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, bool _ismodify = false)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);//当前应用程序的配置文件
if (config != null)
{
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
if (appSettings.Settings.AllKeys.Contains(name))
{
if (_ismodify)
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;
}
}