123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Linq;
- using System.Runtime.Remoting;
- using System.Text;
- using System.Threading.Tasks;
- public static class AppConfigHelper
- {
- public static string Get(string key, string defaultVal = "")
- {
- var str = ConfigurationManager.AppSettings[key];
- if (string.IsNullOrWhiteSpace(str))
- return defaultVal;
- return str.Trim();
- }
- /// <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 Get<T>(string key, T defaultVal = default)
- {
- var str = 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;
- }
- }
- /// <summary>
- /// 如果参数小于0则返回0,否则返回原数字
- /// </summary>
- /// <param name="this"></param>
- /// <returns></returns>
- public static int NotLessThanZero(this int @this)
- {
- if (@this >= 0) return @this;
- return 0;
- }
- /// <summary>
- /// 为url字符串添加后缀
- /// </summary>
- /// <param name="this"></param>
- /// <param name="suffix"></param>
- /// <returns></returns>
- public static string AppendUrlSuffix(this string @this, string suffix)
- {
- if (suffix.StartsWith("/"))
- suffix = suffix.Substring(1);
- if (@this.EndsWith("/"))
- return $"{@this}{suffix}";
- else
- return $"{@this}/{suffix}";
- }
- 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 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");
- }
- }
|