CHistoryPrj.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 
  2. using OTSDataType;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. namespace OTSCommon
  11. {
  12. public class CHistoryPrj : ISlo
  13. {
  14. private string strfilepath = @".\Config\ProData\HistoryPrj.xml";
  15. List<string> m_listPrjPath;
  16. public CHistoryPrj()
  17. {
  18. m_listPrjPath = new List<string>();
  19. }
  20. public void AddPrjPath(string strPath)
  21. {
  22. if (!m_listPrjPath.Contains(strPath))
  23. {
  24. m_listPrjPath.Add(strPath);
  25. }
  26. else
  27. {
  28. int i=m_listPrjPath.IndexOf(strPath);
  29. m_listPrjPath.RemoveAt(i);
  30. m_listPrjPath.Add(strPath);
  31. }
  32. if (m_listPrjPath.Count > 10)
  33. {
  34. m_listPrjPath.RemoveAt(0);
  35. }
  36. }
  37. public List<string> GetPrjPathList()
  38. {
  39. List< string> strTemp= new List<string>(m_listPrjPath);
  40. strTemp.Reverse();
  41. return strTemp;
  42. }
  43. public string GetLatestPrjFolder()
  44. {
  45. string strLatestFolder = string.Empty;
  46. if (m_listPrjPath.Count > 0)
  47. {
  48. strLatestFolder = m_listPrjPath[m_listPrjPath.Count - 1];
  49. int nLastIndex = strLatestFolder.LastIndexOf('\\');
  50. if (nLastIndex != -1)
  51. {
  52. strLatestFolder = strLatestFolder.Substring(0, nLastIndex);
  53. return strLatestFolder;
  54. }
  55. }
  56. return string.Empty;
  57. }
  58. public string GetLatestPrjUpFolder()
  59. {
  60. string strLatestPrj = GetLatestPrjFolder();
  61. string strLatestFolder;
  62. int nLastIndex = strLatestPrj.LastIndexOf('\\');
  63. if (nLastIndex != -1)
  64. {
  65. strLatestFolder = strLatestPrj.Substring(0, nLastIndex);
  66. return strLatestFolder;
  67. }
  68. return string.Empty;
  69. }
  70. public override void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  71. {
  72. if (isStoring)
  73. {
  74. foreach (string strPath in m_listPrjPath)
  75. {
  76. rootNode.AppendChild(classDoc.CreateElement("PathName")).InnerText = strPath;
  77. }
  78. }
  79. else
  80. {
  81. m_listPrjPath.Clear();
  82. for (int i = 0; i < rootNode.ChildNodes.Count; i++)
  83. {
  84. m_listPrjPath.Add(rootNode.ChildNodes[i].InnerText);
  85. }
  86. }
  87. }
  88. public void loadHistoryPath()
  89. {
  90. if (File.Exists(strfilepath))
  91. {
  92. XmlDocument doc = new XmlDocument();
  93. //载入xml文件
  94. doc.Load(strfilepath);
  95. XmlNode root = doc.SelectSingleNode("XMLData");
  96. // tried to load it
  97. Serialize(false, doc, root);
  98. }
  99. }
  100. public void saveHistoryPath()
  101. {
  102. XmlDocument doc = new XmlDocument();
  103. doc.Load(strfilepath);
  104. doc.RemoveAll();
  105. //添加xml文件头申明
  106. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  107. doc.AppendChild(xmldecl);
  108. XmlElement rootNode = doc.CreateElement("XMLData");
  109. doc.AppendChild(rootNode);
  110. Serialize(true, doc, rootNode);
  111. try
  112. {
  113. doc.Save(strfilepath);
  114. }
  115. catch (Exception ex) { }
  116. }
  117. }
  118. }