ConfigFile.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.IO;
  8. using FileManager;
  9. namespace MeasureData
  10. {
  11. public class ConfigFile : ISlo
  12. {
  13. #region 内容
  14. //文件名
  15. private string m_fileName;
  16. public string FileName
  17. {
  18. get { return this.m_fileName; }
  19. set { this.m_fileName = value; }
  20. }
  21. //文件路径
  22. private string m_filepath;
  23. public string FilePath
  24. {
  25. get { return this.m_filepath; }
  26. set { this.m_filepath = value; }
  27. }
  28. public MeasureParam m_Config;
  29. #endregion
  30. public ConfigFile(MeasureParam mp)
  31. {
  32. this.m_Config = mp;
  33. }
  34. /// <summary>
  35. /// 创建XML文件
  36. /// </summary>
  37. /// <param name="filename">创建XML文件的全路径</param>
  38. /// <returns>0:失败;1:成功;2:文件已存在</returns>
  39. public int CreateXml(String filename)
  40. {
  41. if (!File.Exists(filename))
  42. {
  43. if (XmlManager.CreateXmlFile(filename))
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49. return 0;
  50. }
  51. }
  52. else
  53. {
  54. XmlManager.CreateXmlFile(filename);
  55. return 2;
  56. }
  57. }
  58. //保存
  59. public bool Save(String filename)
  60. {
  61. CreateXml(filename);
  62. XmlDocument doc = new XmlDocument();
  63. if (File.Exists(filename))
  64. {
  65. doc.Load(filename);//载入xml文件
  66. }
  67. else
  68. {
  69. return false; //当前路径不存在
  70. }
  71. XmlNode root = doc.SelectSingleNode("XMLData");
  72. Serialize(true, doc, root);
  73. doc.Save(filename);
  74. return true;
  75. }
  76. //读取
  77. public bool Read(String filename)
  78. {
  79. XmlDocument doc = new XmlDocument();
  80. if (File.Exists(filename))
  81. {
  82. doc.Load(filename);//载入xml文件
  83. }
  84. else
  85. {
  86. return false; //当前路径不存在
  87. }
  88. XmlNode root = doc.SelectSingleNode("XMLData");
  89. Serialize(false, doc, root);
  90. return true;
  91. }
  92. //XML文件保存
  93. //样品孔存储xml文档
  94. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  95. {
  96. Slo sFile = new Slo();
  97. xString filename = new xString();
  98. xString filepath = new xString();
  99. filename.AssignValue(this.m_fileName);
  100. filepath.AssignValue(this.m_filepath);
  101. sFile.Register("FileName", filename);
  102. sFile.Register("FilePath", filepath);
  103. sFile.Register("Param", this.m_Config);
  104. if (isStoring)
  105. {
  106. sFile.Serialize(true, xml, rootNode);
  107. }
  108. else
  109. {
  110. sFile.Serialize(false, xml, rootNode);
  111. this.m_fileName = filename.value();
  112. this.m_filepath = filepath.value();
  113. }
  114. }
  115. }
  116. }