CConfigManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Xml.Serialization;
  3. using System.IO;
  4. using System.Collections.Concurrent;
  5. using System.Drawing;
  6. using System.Collections.Generic;
  7. namespace PMSUI.Config
  8. {
  9. /// <summary>
  10. /// 全局数据
  11. /// </summary>
  12. [Serializable]
  13. public class CGlobalData
  14. {
  15. public bool gs_IsSavePassword = false;
  16. public string gs_UserName = "";
  17. public string gs_Password = "";
  18. public byte gs_FormBackColor_R = Color.White.R;
  19. public byte gs_FormBackColor_G = Color.White.G;
  20. public byte gs_FormBackColor_B = Color.White.B;
  21. public bool gs_IsShowMaterial = false; // 是否显示物料信息
  22. public bool gs_IsUseBatchOpt = false; // 是否批量操作
  23. public string gs_DisplayLanguage = "zh-CN"; // 界面显示语言
  24. }
  25. public class CConfigManager
  26. {
  27. public static CGlobalData gs_globalConfig = new CGlobalData();
  28. public static CFormHelpConfig gs_FormHelpConfig = new CFormHelpConfig();
  29. private static string m_strPath = "";
  30. private static string _GlobalDataFilePath = "";
  31. private static string _FormHelpFilePath = "";
  32. public static void Init()
  33. {
  34. InitConfigPath();
  35. InitGlobalData();
  36. InitFormHelp();
  37. }
  38. private static void InitConfigPath()
  39. {
  40. //获取到文件完整路径
  41. string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
  42. //获取到文件所在目录
  43. FileInfo finfo = new FileInfo(s);
  44. string BaseDirectory = finfo.DirectoryName;
  45. m_strPath = Path.Combine(BaseDirectory, "PMS", "Config");
  46. _GlobalDataFilePath = Path.Combine(m_strPath, "CConfigSetting.xml");
  47. _FormHelpFilePath = Path.Combine(m_strPath, "FormHelpConfig.xml");
  48. }
  49. private static void ExsitConfigPath()
  50. {
  51. DirectoryInfo dir = new DirectoryInfo(m_strPath);
  52. if (!dir.Exists)
  53. {
  54. dir.Create();
  55. }
  56. }
  57. public static void InitGlobalData()
  58. {
  59. //反序列化
  60. try
  61. {
  62. ExsitConfigPath();
  63. XmlSerializer serializer = new XmlSerializer(gs_globalConfig.GetType());
  64. if(!File.Exists(_GlobalDataFilePath))
  65. {
  66. SaveGlobalData();
  67. }
  68. else
  69. {
  70. using (TextReader tr = new StreamReader(_GlobalDataFilePath))
  71. {
  72. gs_globalConfig = (CGlobalData)serializer.Deserialize(tr);
  73. }
  74. }
  75. }
  76. catch (Exception e)
  77. {
  78. string strlog = e.Message;
  79. }
  80. }
  81. public static void SaveGlobalData()
  82. {
  83. try
  84. {
  85. ExsitConfigPath();
  86. XmlSerializer serializer = new XmlSerializer(gs_globalConfig.GetType());
  87. using (TextWriter tw = new StreamWriter(_GlobalDataFilePath))
  88. {
  89. serializer.Serialize(tw, gs_globalConfig);
  90. }
  91. }
  92. catch
  93. { }
  94. }
  95. public static void InitFormHelp()
  96. {
  97. //反序列化
  98. try
  99. {
  100. ExsitConfigPath();
  101. if (!File.Exists(_FormHelpFilePath))
  102. {
  103. gs_FormHelpConfig.FormHelps = DefaultFormHelp.FormHelps();
  104. SaveFormHelp();
  105. }
  106. else
  107. {
  108. XmlSerializer serializer = new XmlSerializer(gs_FormHelpConfig.GetType());
  109. using (TextReader tr = new StreamReader(_FormHelpFilePath))
  110. {
  111. gs_FormHelpConfig = (CFormHelpConfig)serializer.Deserialize(tr);//Serialize(tw, CConfigData.gs_globalConfig)
  112. }
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. string strlog = e.Message;
  118. }
  119. }
  120. public static void SaveFormHelp()
  121. {
  122. try
  123. {
  124. ExsitConfigPath();
  125. XmlSerializer serializer = new XmlSerializer(gs_FormHelpConfig.GetType());
  126. using (TextWriter tw = new StreamWriter(_FormHelpFilePath))
  127. {
  128. serializer.Serialize(tw, gs_FormHelpConfig);
  129. }
  130. }
  131. catch (Exception e)
  132. {
  133. string strlog = e.Message;
  134. }
  135. }
  136. }
  137. }