[c#]代码库
public class ConfigHelper
{
#region 读写配置文件
public static string GetConfigFileName()
{
return System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;
}
/// <summary>
/// 修改配置文件中某项的值
/// </summary>
/// <param name="key">appSettings的key</param>
/// <param name="value">appSettings的Value</param>
public static void SetConfig(string key, string value)
{
string path=GetConfigFileName();
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
config.AppSettings.Settings[key].Value = value;
//if (config.AppSettings.Settings[key] != null)
// config.AppSettings.Settings[key].Value = value;
//else
// config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 读取配置文件某项的值
/// </summary>
/// <param name="key">appSettings的key</param>
/// <returns>appSettings的Value</returns>
public static string GetConfig(string key)
{
string _value = string.Empty;
string path = GetConfigFileName();
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
if (config.AppSettings.Settings[key] != null)
{
_value = config.AppSettings.Settings[key].Value;
}
return _value;
}
#endregion
/// <summary>
/// 数据服务连接方式 WebApiUri:ConnectString
/// </summary>
public static string ServerType
{
get
{
return GetConfig("ServerType");
}
set
{
SetConfig("ServerType", value);
}
}
/// <summary>
/// WebApi地址
/// </summary>
public static string WebApiUri
{
get
{
return GetConfig("WebApiUri");
}
set
{
SetConfig("WebApiUri", value);
}
}
/// <summary>
/// 数据库连接字符串
/// </summary>
public static string ConnectString
{
get
{
return GetConfig("ConnectString");
}
set
{
SetConfig("ConnectString", value);
}
}
/// <summary>
/// 程序更新地址
/// </summary>
public static string UpdateUri
{
get
{
return GetConfig("UpdateUri");
}
set
{
SetConfig("UpdateUri", value);
}
}
/// <summary>
/// 登录用户名
/// </summary>
public static string LoginUserName
{
get
{
return GetConfig("LoginUserName");
}
set
{
SetConfig("LoginUserName", value);
}
}
/// <summary>
/// 密码
/// </summary>
public static string UserPasswd
{
get
{
return GetConfig("UserPasswd");
}
set
{
SetConfig("UserPasswd", value);
}
}
/// <summary>
/// 默认语言
/// </summary>
public static string Lang
{
get
{
return GetConfig("Lang");
}
set
{
SetConfig("Lang", value);
}
}
/// <summary>
/// 默认语言文件位置
/// </summary>
public static string LangFile
{
get
{
return GetConfig("LangFile");
}
set
{
SetConfig("LangFile", value);
}
}
}
by: 发表于:2017-12-20 17:30:04 顶(0) | 踩(0) 回复
??
回复评论