You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SLH.SSDMS.Common
{
public class ConfigHelper
{
/// <summary>
/// 获取配置
/// </summary>
/// <param name="sections">节点配置</param>
/// <returns></returns>
public static string GetConfig(string sections)
{
try
{
return ConfigurationManager.AppSettings[sections].ToString();
}
catch (Exception) { }
return "";
}
/// <summary>
/// 更新Config文件若Key已存在替换当前Val值不存在则添加新的键值对
/// </summary>
/// <param name="str_newKey"></param>
/// <param name="str_newVal"></param>
public static void UpdateAppConfig(string key, string value)
{
//增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value = value;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
}
}
}