Form1.cs 9.6 KB

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