50 lines
2.1 KiB
C#
50 lines
2.1 KiB
C#
using System.Configuration;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace ShrlAlgo.Toolkit.Core.Heplers
|
|
{
|
|
public class ConfigHelpers
|
|
{
|
|
/// <summary>
|
|
/// 获取项目文件夹下的配置文件根据Key获取Value
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public static string GetCfgValue(string key)
|
|
{
|
|
var assembly = Assembly.GetCallingAssembly();
|
|
|
|
var uri = new Uri(Path.GetDirectoryName(assembly.Location) ?? string.Empty);
|
|
var map = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".dll.config") };
|
|
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
|
|
var loadpath = config.AppSettings.Settings[key].Value; //若路径存在则自动获取该路径
|
|
return loadpath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存到程序目录下的配置文件
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
public static void SetCfgPairs(string key, string value)
|
|
{
|
|
var assembly = Assembly.GetCallingAssembly();
|
|
var uri = new Uri(Path.GetDirectoryName(assembly.Location) ?? string.Empty);
|
|
var map = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".dll.config") };
|
|
|
|
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
|
|
config.AppSettings.Settings[key].Value = value; //将用户选取的路径_path赋给app.config中的_path(名称自取)
|
|
//ConfigurationManager.OpenMappedExeConfiguration(map, 0).AppSettings.Settings["FamilyPath"].Value = SelectedPath;
|
|
config.AppSettings.SectionInformation.ForceSave = true;
|
|
|
|
config.Save(ConfigurationSaveMode.Modified); //将配置保存
|
|
ConfigurationManager.RefreshSection("appSettings"); //刷新配置文件
|
|
//if (Directory.Exists(config.AppSettings.Settings["FamilyPath"].Value))//判断配置的路径是否存在
|
|
//{
|
|
// dialog.SelectedPath = config.AppSettings.Settings["FamilyPath"].Value;//若路径存在则自动获取该路径
|
|
//}
|
|
}
|
|
}
|
|
}
|