| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //************************************************************************
- // author: yuzhengyang
- // date: 2018.3.27 - 2018.6.3
- // desc: 工具描述
- // Copyright (c) yuzhengyang. All rights reserved.
- //************************************************************************
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- namespace Azylee.Core.IOUtils.TxtUtils
- {
- /// <summary>
- /// .NET 读取配置工具
- /// </summary>
- public class ConfigTool
- {
- /// <summary>
- /// 获取配置值
- /// </summary>
- /// <param name="key"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static string Get(string key, string defaultValue = "")
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- return config.AppSettings.Settings[key]?.Value ?? defaultValue;
- }
- catch { return defaultValue; }
- }
- /// <summary>
- /// 获取配置值(bool)
- /// </summary>
- /// <param name="key"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static bool GetBool(string key, bool defaultValue = false)
- {
- string value = Get(key, "");
- if (Str.Ok(value))
- {
- value = value.ToLower();
- switch (value)
- {
- case "1":
- case "ok":
- case "yes":
- case "true":
- case "enable":
- case "access": return true;
- default: return false;
- }
- }
- return defaultValue;
- }
- public static string GetExe(string exePath, string key, string defaultValue = "")
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
- return config.AppSettings.Settings[key]?.Value ?? defaultValue;
- }
- catch { return defaultValue; }
- }
- public static int GetInt(string key, int defaultValue = 0)
- {
- string s = Get(key: key);
- if (int.TryParse(s, out int value)) return value;
- return defaultValue;
- }
- public static int GetExeInt(string exePath, string key, int defaultValue = 0)
- {
- string s = GetExe(exePath: exePath, key: key);
- if (int.TryParse(s, out int value)) return value;
- return defaultValue;
- }
- public static bool Set(string key, string value)
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- config.AppSettings.Settings[key].Value = value;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- return true;
- }
- catch { return false; }
- }
- public static bool SetExe(string exePath, string key, string value)
- {
- try
- {
- Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
- config.AppSettings.Settings[key].Value = value;
- config.Save(ConfigurationSaveMode.Modified);
- ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
- return true;
- }
- catch { return false; }
- }
- //private bool CanUpdate()
- //{
- // string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
- // string key = "TodayUpdateTimes";
- // DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
- // DateTime setday = today;
- // //读取配置
- // string temp = CanUpdateGetConfig(file, key);
- // if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
- // {
- // if (setday.Hour < 3)
- // CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
- // else
- // return false;
- // }
- // else
- // {
- // //配置失效,设置为默认值
- // CanUpdateSetConfig(file, key, today.ToString());
- // }
- // return true;
- //}
- //private bool CanUpdateSetConfig(string file, string key, string value)
- //{
- // try
- // {
- // //文件不存在则创建
- // if (!File.Exists(file + ".config"))
- // {
- // XElement xe = new XElement("configuration");
- // xe.Save(file + ".config");
- // }
- // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
- // if (config.AppSettings.Settings.AllKeys.Contains(key))
- // {
- // config.AppSettings.Settings[key].Value = value;
- // }
- // else
- // {
- // config.AppSettings.Settings.Add(key, value);
- // }
- // config.Save(ConfigurationSaveMode.Modified);
- // return true;
- // }
- // catch (Exception e)
- // {
- // return false;
- // }
- //}
- //private string CanUpdateGetConfig(string file, string key)
- //{
- // try
- // {
- // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
- // if (config.AppSettings.Settings.AllKeys.Contains(key))
- // {
- // return config.AppSettings.Settings[key].Value;
- // }
- // }
- // catch (Exception e) { }
- // return null;
- //}
- }
- }
|