| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Configuration;
- using System.IO;
- using System.ComponentModel;
- using Ips.Library.Basic;
- namespace Ips.Library.DxpLib
- {
- public class ToolConfig
- {
- public static string GetAppSetting(string key, string defaultVal = "")
- {
- string val = ConfigurationManager.AppSettings[key];
- return val.IfNullOrWhitespace(defaultVal);
- }
- /// <summary>
- /// 获取App.config配置文件中appSettings节点中指定key的value
- /// <para>如果泛型为bool,则{"1","true","True","TRUE"}都会被转换成true,否则转换为false</para>
- /// <para>该方法支持int?、double?等可空类型的转换</para>
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <param name="defaultVal"></param>
- /// <returns></returns>
- public static T GetAppSetting<T>(string key, T defaultVal = default)
- {
- var str = System.Configuration.ConfigurationManager.AppSettings[key];
- if (string.IsNullOrWhiteSpace(str))
- {
- return defaultVal;
- }
- str = str.Trim();
- bool isNullable = typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>);
- if (isNullable)
- {
- NullableConverter nullableConverter = new NullableConverter(typeof(T));
- return (T)nullableConverter.ConvertFromString(str);
- }
- else if (typeof(T) == typeof(bool))
- {
- if (str.ToLower().Trim() == "true" || str.ToLower().Trim() == "1")
- {
- T boolVal = (T)Convert.ChangeType(true, typeof(T));
- return boolVal;
- }
- else
- {
- T boolVal = (T)Convert.ChangeType(false, typeof(T));
- return boolVal;
- }
- }
- else
- {
- T ret = (T)Convert.ChangeType(str, typeof(T));
- return ret;
- }
- }
- public static void SetAppSetting(string key, string value)
- {
- Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- if (ConfigurationManager.AppSettings[key] == null)
- {
- configuration.AppSettings.Settings.Add(key, value);
- }
- else
- {
- configuration.AppSettings.Settings[key].Value = value;
- }
- RefreshAndSaveSection(configuration);
- }
- public static T GetAppSetting<T>(string key) where T : struct
- {
- var @string = ConfigurationManager.AppSettings[key];
- var td = TypeDescriptor.GetConverter(typeof(T));
- return (T)td.ConvertFromInvariantString(@string);
- }
- public static void SetAppSetting<T>(string key, T value) where T : struct
- {
- var td = TypeDescriptor.GetConverter(typeof(T));
- var @string = td.ConvertToInvariantString(value);
- SetAppSetting(key, @string);
- }
- public static void SetAppSetting(Dictionary<string, string> settings)
- {
- if (settings == null || settings.Count == 0) return;
- Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- foreach (var setting in settings)
- {
- string key = setting.Key;
- string value = setting.Value;
- if (ConfigurationManager.AppSettings[key] == null)
- {
- configuration.AppSettings.Settings.Add(key, value);
- }
- else
- {
- configuration.AppSettings.Settings[key].Value = value;
- }
- }
- RefreshAndSaveSection(configuration);
- }
- public static void RefreshAndSaveSection(Configuration config)
- {
- string configPath = config.FilePath;
- FileInfo fileInfo = new FileInfo(configPath);
- fileInfo.Attributes = FileAttributes.Normal;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");
- }
- }
- }
|