Form1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. using System.Xml;
  6. namespace UAS_XmlAnalysor
  7. {
  8. public partial class Form1 : Form
  9. {
  10. DataHelper dh = new DataHelper();
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17. FolderPath.Text = Properties.Settings.Default.FolderPath;
  18. BackUpFolderPath.Text = Properties.Settings.Default.BackUpFolderPath;
  19. }
  20. private void StartWatch_Click(object sender, EventArgs e)
  21. {
  22. if (!dh.CheckExist("source", "sc_code='" + Source.Text + "' and sc_statuscode='AUDITED'"))
  23. {
  24. OperateResult.AppendText("岗位资源错误或者未审核\n");
  25. return;
  26. }
  27. XmlWatcher.Path = FolderPath.Text;
  28. XmlWatcher.Filter = "*.xml";
  29. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  30. XmlWatcher.EnableRaisingEvents = true;
  31. Properties.Settings.Default.FolderPath = FolderPath.Text;
  32. Properties.Settings.Default.BackUpFolderPath = BackUpFolderPath.Text;
  33. Properties.Settings.Default.Save();
  34. Source.Enabled = false;
  35. OperateResult.AppendText("开始执行监控\n");
  36. }
  37. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  38. {
  39. while (true)
  40. {
  41. try
  42. {
  43. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  44. {
  45. if (stream != null)
  46. break;
  47. }
  48. System.Threading.Thread.Sleep(500);
  49. }
  50. catch (Exception)
  51. {
  52. }
  53. }
  54. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  55. string sncode = e.Name.Split('.')[0];
  56. OperateResult.AppendText("读取文件" + e.Name + "\n");
  57. List<string> name = new List<string>();
  58. List<string> result = new List<string>();
  59. int name_or_result = 0;
  60. while (myReader.Read())
  61. {
  62. if (myReader.NodeType == XmlNodeType.Text)
  63. {
  64. if (name_or_result % 2 == 0)
  65. {
  66. name.Add(myReader.Value);
  67. name_or_result++;
  68. }
  69. else
  70. {
  71. result.Add(myReader.Value);
  72. name_or_result++;
  73. }
  74. }
  75. }
  76. string sql = "insert into STEPTESTDETAIL(std_id,std_sn,std_subclass1,std_testresult,std_date,std_rescode) values (STEPTESTDETAIL_seq.nextval,'" + sncode + "',:std_subclass1,:std_testresult,sysdate,'" + Source.Text + "')";
  77. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  78. myReader.Close();
  79. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  80. if (file.Exists)
  81. {
  82. try
  83. {
  84. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  85. }
  86. catch (Exception ex)
  87. {
  88. Console.WriteLine(ex.Message);
  89. }
  90. }
  91. }
  92. private void StopWatch_Click(object sender, EventArgs e)
  93. {
  94. XmlWatcher.EnableRaisingEvents = false;
  95. Source.Enabled = true;
  96. OperateResult.AppendText("停止执行监控\n");
  97. }
  98. private void Clean_Click(object sender, EventArgs e)
  99. {
  100. OperateResult.Clear();
  101. }
  102. private void ChooseFolder_Click(object sender, EventArgs e)
  103. {
  104. FolderBrowserDialog folder = new FolderBrowserDialog();
  105. folder.Description = "选择监控文件夹";
  106. DialogResult result = folder.ShowDialog();
  107. if (result == DialogResult.OK)
  108. {
  109. FolderPath.Text = folder.SelectedPath;
  110. }
  111. }
  112. private void ReadNodeFromXML(string FileName)
  113. {
  114. }
  115. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  116. {
  117. FolderBrowserDialog folder = new FolderBrowserDialog();
  118. folder.Description = "选择备份文件夹";
  119. DialogResult result = folder.ShowDialog();
  120. if (result == DialogResult.OK)
  121. {
  122. BackUpFolderPath.Text = folder.SelectedPath;
  123. }
  124. }
  125. }
  126. }