Form1.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. namespace UAS_XmlAnalysor
  9. {
  10. public partial class Form1 : Form
  11. {
  12. DataHelper dh = new DataHelper();
  13. DataTable dt;
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. FolderPath.Text = Properties.Settings.Default.FolderPath;
  21. BackUpFolderPath.Text = Properties.Settings.Default.BackUpFolderPath;
  22. Source.Text = Properties.Settings.Default.Source;
  23. Master.Text = Properties.Settings.Default.Master;
  24. AutoStart.Checked = Properties.Settings.Default.AutoStart;
  25. dt = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address from master ", "select");
  26. Master.DataSource = dt;
  27. Master.DisplayMember = "ma_user";
  28. Master.ValueMember = "ma_user";
  29. StartWatch.PerformClick();
  30. }
  31. private void StartWatch_Click(object sender, EventArgs e)
  32. {
  33. for (int i = 0; i < dt.Rows.Count; i++)
  34. {
  35. if (Master.Text == dt.Rows[i]["ma_user"].ToString())
  36. {
  37. DataHelper.DBConnectionString = "Data Source=" + dt.Rows[i]["ma_address"] + ";User ID=" + dt.Rows[i]["ma_user"] + ";PassWord=" + dt.Rows[i]["ms_pwd"]; ;
  38. Console.WriteLine(DataHelper.DBConnectionString);
  39. dh = new DataHelper();
  40. }
  41. }
  42. if (!dh.CheckExist("source", "sc_code='" + Source.Text + "' and sc_statuscode='AUDITED'"))
  43. {
  44. OperateResult.AppendText("岗位资源错误或者未审核\n");
  45. return;
  46. }
  47. XmlWatcher.Path = FolderPath.Text;
  48. XmlWatcher.Filter = "*.xml";
  49. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  50. XmlWatcher.EnableRaisingEvents = true;
  51. Properties.Settings.Default.FolderPath = FolderPath.Text;
  52. Properties.Settings.Default.BackUpFolderPath = BackUpFolderPath.Text;
  53. Properties.Settings.Default.Source = Source.Text;
  54. Properties.Settings.Default.Master = Master.Text;
  55. Properties.Settings.Default.AutoStart = AutoStart.Checked;
  56. Properties.Settings.Default.Save();
  57. Source.Enabled = false;
  58. StartWatch.Enabled = false;
  59. ChooseFolder.Enabled = false;
  60. ChooseBackUpFolder.Enabled = false;
  61. SetAutoRun();
  62. OperateResult.AppendText("开始执行监控\n");
  63. }
  64. public void SetAutoRun()
  65. {
  66. if (AutoStart.Checked) //设置开机自启动
  67. {
  68. string path = Application.ExecutablePath;
  69. RegistryKey rk = Registry.LocalMachine;
  70. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  71. rk2.SetValue("UAS_XML解析器.exe", path);
  72. rk2.Close();
  73. rk.Close();
  74. }
  75. else //取消开机自启动
  76. {
  77. string path = Application.ExecutablePath;
  78. RegistryKey rk = Registry.LocalMachine;
  79. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  80. rk2.DeleteValue("UAS_XML解析器.exe", false);
  81. rk2.Close();
  82. rk.Close();
  83. }
  84. }
  85. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  86. {
  87. while (true)
  88. {
  89. try
  90. {
  91. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  92. {
  93. if (stream != null)
  94. break;
  95. }
  96. System.Threading.Thread.Sleep(500);
  97. }
  98. catch (Exception) { }
  99. }
  100. string testDate = "";
  101. string testTime = "";
  102. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  103. string sncode = e.Name.Split('.')[0];
  104. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "'").ToString();
  105. OperateResult.AppendText("读取文件" + e.Name + "\n");
  106. List<string> name = new List<string>();
  107. List<string> result = new List<string>();
  108. int name_or_result = 0;
  109. while (myReader.Read())
  110. {
  111. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
  112. {
  113. testDate = myReader.GetAttribute(1);
  114. testTime = myReader.GetAttribute(0);
  115. }
  116. if (myReader.NodeType == XmlNodeType.Text)
  117. {
  118. if (name_or_result % 2 == 0)
  119. {
  120. name.Add(myReader.Value);
  121. name_or_result++;
  122. }
  123. else
  124. {
  125. result.Add(myReader.Value);
  126. name_or_result++;
  127. }
  128. }
  129. }
  130. string date = testDate + " " + testTime;
  131. string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
  132. sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
  133. sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + Source.Text + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
  134. sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + date + "','YYYY/MM/DD hh24:mi:ss'))";
  135. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  136. myReader.Close();
  137. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  138. if (file.Exists)
  139. {
  140. try
  141. {
  142. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  143. }
  144. catch (Exception ex)
  145. {
  146. OperateResult.AppendText(e.Name + ex.Message + "\n");
  147. }
  148. }
  149. }
  150. private void StopWatch_Click(object sender, EventArgs e)
  151. {
  152. XmlWatcher.EnableRaisingEvents = false;
  153. Source.Enabled = true;
  154. StartWatch.Enabled = true;
  155. ChooseFolder.Enabled = true;
  156. ChooseBackUpFolder.Enabled = true;
  157. OperateResult.AppendText("停止执行监控\n");
  158. }
  159. private void Clean_Click(object sender, EventArgs e)
  160. {
  161. OperateResult.Clear();
  162. }
  163. private void ChooseFolder_Click(object sender, EventArgs e)
  164. {
  165. FolderBrowserDialog folder = new FolderBrowserDialog();
  166. folder.Description = "选择监控文件夹";
  167. DialogResult result = folder.ShowDialog();
  168. if (result == DialogResult.OK)
  169. {
  170. FolderPath.Text = folder.SelectedPath;
  171. }
  172. }
  173. private void ReadNodeFromXML(string FileName)
  174. {
  175. }
  176. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  177. {
  178. FolderBrowserDialog folder = new FolderBrowserDialog();
  179. folder.Description = "选择备份文件夹";
  180. DialogResult result = folder.ShowDialog();
  181. if (result == DialogResult.OK)
  182. {
  183. BackUpFolderPath.Text = folder.SelectedPath;
  184. }
  185. }
  186. }
  187. }