AppConfig.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. namespace CommonTool
  2. {
  3. public class AppConfig
  4. {
  5. public AppConfig(string pcConfigFilePath)
  6. {
  7. _configFile = pcConfigFilePath;
  8. }
  9. private void LoadSetting()
  10. {
  11. string text1 = FileFuns.ReadFileAsString(FileName);
  12. if (text1 != "")
  13. {
  14. _settings = SnXmlNode.ParseGenericXml(text1);
  15. }
  16. }
  17. public bool ReadBool(string pcConfigId)
  18. {
  19. return UtilStr.StrToBool(ReadString(pcConfigId));
  20. }
  21. public int ReadInt(string pcConfigId)
  22. {
  23. return Utils.ValI(ReadString(pcConfigId));
  24. }
  25. public decimal ReadDouble(string pcConfigId)
  26. {
  27. return Utils.ValD(ReadString(pcConfigId));
  28. }
  29. public string ReadString(string pcConfigId)
  30. {
  31. return Settings.GetChildValue(pcConfigId);
  32. }
  33. public SnXmlNode ReadNode(string pcConfigId)
  34. {
  35. return Settings.GetChildNode(pcConfigId);
  36. }
  37. public string ReadString(string pcConfigId, string defaultValue)
  38. {
  39. string lcStr = Settings.GetChildValue(pcConfigId);
  40. if (string.IsNullOrEmpty(lcStr))
  41. {
  42. lcStr = defaultValue;
  43. }
  44. return lcStr;
  45. }
  46. public void SaveSetting()
  47. {
  48. FileFuns.WriteStringToFile(Settings.ToAllXmlString(), FileName);
  49. }
  50. public void SetString(string pcConfigId, string pcValue)
  51. {
  52. string text1 = UtilStr.UAndT(pcConfigId);
  53. Settings.SetChildValue(text1, pcValue);
  54. SaveSetting();
  55. }
  56. //读取指定文件的配置信息,保存到当前的配置文件中
  57. public void ResetSetting(string pcString)
  58. {
  59. if (pcString != "")
  60. {
  61. _settings = SnXmlNode.ParseGenericXml(pcString);
  62. }
  63. else
  64. {
  65. LoadSetting();
  66. }
  67. SaveSetting();
  68. }
  69. //重新装载配置信息
  70. public void RelandConfig(string pcPath)
  71. {
  72. LoadSetting();
  73. }
  74. #region Properties
  75. private string FileName
  76. {
  77. get
  78. {
  79. if (_configFile == "")
  80. {
  81. _configFile = AppEnv.ConfigFile;
  82. }
  83. return _configFile;
  84. }
  85. }
  86. string _configFile;
  87. public SnXmlNode Settings
  88. {
  89. get
  90. {
  91. if (_settings == null)
  92. {
  93. LoadSetting();
  94. }
  95. return _settings;
  96. }
  97. }
  98. #endregion
  99. #region Fields
  100. private SnXmlNode _settings;
  101. #endregion
  102. static AppConfig _defConfig;
  103. public static AppConfig DefConfig => _defConfig ?? (_defConfig = new AppConfig(""));
  104. static AppConfig _languages;
  105. public static AppConfig Languages
  106. {
  107. get
  108. {
  109. if (_languages != null) return _languages;
  110. _languages = new AppConfig(DefConfig.ReadString("LanguagesPath"));
  111. return _languages;
  112. }
  113. }
  114. }
  115. }