Pub.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. namespace Jpsoft.BLL
  7. {
  8. public class Pub
  9. {
  10. /// <summary>
  11. /// 修改配置文件中某项的值
  12. /// </summary>
  13. /// <param name="key">appSettings的key</param>
  14. /// <param name="value">appSettings的Value</param>
  15. public static void SetConfig(string key, string value)
  16. {
  17. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  18. if (config.AppSettings.Settings[key] != null)
  19. config.AppSettings.Settings[key].Value = value;
  20. else
  21. config.AppSettings.Settings.Add(key, value);
  22. config.Save(ConfigurationSaveMode.Modified);
  23. ConfigurationManager.RefreshSection("appSettings");
  24. }
  25. /// <summary>
  26. /// 读取配置文件某项的值
  27. /// </summary>
  28. /// <param name="key">appSettings的key</param>
  29. /// <returns>appSettings的Value</returns>
  30. public static string GetConfig(string key)
  31. {
  32. string _value = string.Empty;
  33. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  34. if (config.AppSettings.Settings[key] != null)
  35. {
  36. _value = config.AppSettings.Settings[key].Value;
  37. }
  38. return _value;
  39. }
  40. }
  41. }