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