ConfigTool.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Y.Utils.IOUtils.TxtUtils
  6. {
  7. class ConfigTool
  8. {
  9. //private bool CanUpdate()
  10. //{
  11. // string file = AppDomain.CurrentDomain.BaseDirectory + "Settings";
  12. // string key = "TodayUpdateTimes";
  13. // DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
  14. // DateTime setday = today;
  15. // //读取配置
  16. // string temp = CanUpdateGetConfig(file, key);
  17. // if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
  18. // {
  19. // if (setday.Hour < 3)
  20. // CanUpdateSetConfig(file, key, setday.AddHours(1).ToString());//累加hour记录次数
  21. // else
  22. // return false;
  23. // }
  24. // else
  25. // {
  26. // //配置失效,设置为默认值
  27. // CanUpdateSetConfig(file, key, today.ToString());
  28. // }
  29. // return true;
  30. //}
  31. //private bool CanUpdateSetConfig(string file, string key, string value)
  32. //{
  33. // try
  34. // {
  35. // //文件不存在则创建
  36. // if (!File.Exists(file + ".config"))
  37. // {
  38. // XElement xe = new XElement("configuration");
  39. // xe.Save(file + ".config");
  40. // }
  41. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  42. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  43. // {
  44. // config.AppSettings.Settings[key].Value = value;
  45. // }
  46. // else
  47. // {
  48. // config.AppSettings.Settings.Add(key, value);
  49. // }
  50. // config.Save(ConfigurationSaveMode.Modified);
  51. // return true;
  52. // }
  53. // catch (Exception e)
  54. // {
  55. // return false;
  56. // }
  57. //}
  58. //private string CanUpdateGetConfig(string file, string key)
  59. //{
  60. // try
  61. // {
  62. // Configuration config = ConfigurationManager.OpenExeConfiguration(file);
  63. // if (config.AppSettings.Settings.AllKeys.Contains(key))
  64. // {
  65. // return config.AppSettings.Settings[key].Value;
  66. // }
  67. // }
  68. // catch (Exception e) { }
  69. // return null;
  70. //}
  71. }
  72. }