Form1.cs 8.4 KB

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