添加项目文件。
This commit is contained in:
52
CleanMachineHMI/Utils/AppConfigHelper.cs
Normal file
52
CleanMachineHMI/Utils/AppConfigHelper.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CleanMachineHMI.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置文件操作
|
||||
/// </summary>
|
||||
public static class AppConfigHelper
|
||||
{
|
||||
public static string Get(string key)
|
||||
{
|
||||
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
return config.AppSettings.Settings[key].Value;
|
||||
}
|
||||
|
||||
public static void Remove(string key)
|
||||
{
|
||||
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
config.AppSettings.Settings.Remove(key);
|
||||
config.Save(ConfigurationSaveMode.Modified);
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
|
||||
public static void Add(string key, string value)
|
||||
{
|
||||
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
config.AppSettings.Settings.Add(key, value);
|
||||
config.Save(ConfigurationSaveMode.Modified);
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
|
||||
public static void Update(string key, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
config.AppSettings.Settings[key].Value = value;
|
||||
config.Save(ConfigurationSaveMode.Modified);
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user