CConfigManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using ProjectManagementSystem.Common.Function;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6. namespace ProjectManagementSystem.Config
  7. {
  8. public class CConfiManager
  9. {
  10. public static CustomConfig customConfig = new CustomConfig();
  11. public static PmsAutoupdateConfig PmsConfig = new PmsAutoupdateConfig();
  12. private static string m_strPath = "";
  13. private static string _GlobalDataFilePath = "";
  14. public static void Init()
  15. {
  16. InitConfigPath();
  17. InitGlobalData();
  18. InitPmsAutoupdateXml();
  19. }
  20. private static void InitConfigPath()
  21. {
  22. //获取到文件完整路径
  23. string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
  24. //获取到文件所在目录
  25. FileInfo finfo = new FileInfo(s);
  26. string BaseDirectory = finfo.DirectoryName;
  27. m_strPath = Path.Combine(BaseDirectory, "PMS", "Config");
  28. _GlobalDataFilePath = Path.Combine(m_strPath, "CConfigSetting.xml");
  29. }
  30. private static void ExsitConfigPath()
  31. {
  32. DirectoryInfo dir = new DirectoryInfo(m_strPath);
  33. if (!dir.Exists)
  34. {
  35. dir.Create();
  36. }
  37. }
  38. public static void InitGlobalData()
  39. {
  40. //反序列化
  41. try
  42. {
  43. ExsitConfigPath();
  44. XmlSerializer serializer = new XmlSerializer(customConfig.GetType());
  45. if (!File.Exists(_GlobalDataFilePath))
  46. {
  47. SaveGlobalData();
  48. }
  49. else
  50. {
  51. using (TextReader tr = new StreamReader(_GlobalDataFilePath))
  52. {
  53. customConfig = (CustomConfig)serializer.Deserialize(tr);
  54. }
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. string strlog = e.Message;
  60. }
  61. }
  62. public static void SaveGlobalData()
  63. {
  64. try
  65. {
  66. ExsitConfigPath();
  67. XmlSerializer serializer = new XmlSerializer(customConfig.GetType());
  68. using (TextWriter tw = new StreamWriter(_GlobalDataFilePath))
  69. {
  70. serializer.Serialize(tw, customConfig);
  71. }
  72. }
  73. catch
  74. { }
  75. }
  76. public static void InitPmsAutoupdateXml()
  77. {
  78. try
  79. {
  80. string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
  81. FileInfo finfo = new FileInfo(s);
  82. string currPath = finfo.DirectoryName;
  83. //string configDir = Path.Combine(currPath, "PMS", "Config");
  84. string configFile = Path.Combine(currPath, "pms.xml");
  85. PmsConfig.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  86. PmsConfig.Url = AppSetting.GetAutoUpdateUrl().Replace("xml", "zip");
  87. string xml = XmlTools.XmlSerialize(PmsConfig);
  88. //XmlSerializer serializer = new XmlSerializer(PmsConfig.GetType());
  89. using (TextWriter tw = new StreamWriter(configFile))
  90. {
  91. tw.Write(xml);
  92. //serializer.Serialize(tw, PmsConfig);
  93. }
  94. string dataFile = Path.Combine(currPath, "pms.zip");
  95. if (File.Exists(dataFile))
  96. {
  97. File.Delete(dataFile);
  98. }
  99. List<string> fileList = new List<string>();
  100. DirectoryInfo theFolder = new DirectoryInfo(currPath);
  101. FileInfo[] fileInfos = theFolder.GetFiles();
  102. //遍历文件
  103. foreach (FileInfo fileInfo in fileInfos)
  104. {
  105. if (fileInfo.Extension == ".config"
  106. || fileInfo.Name.Contains("vshost"))
  107. {
  108. continue;
  109. }
  110. if (fileInfo.Name.StartsWith("ProjectManagementSystem")
  111. || fileInfo.Name.StartsWith("DbCommon"))
  112. {
  113. fileList.Add(fileInfo.FullName);
  114. }
  115. }
  116. ZipUtility zipUtility = new ZipUtility();
  117. zipUtility.ZipFileFromFiles(currPath, fileList, dataFile, 9);
  118. }
  119. catch (Exception)
  120. {
  121. }
  122. }
  123. }
  124. }