Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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;
  14. DataTable dt;
  15. Thread InitDB;
  16. string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/Cache/Cache.xml";
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. StartPosition = FormStartPosition.CenterScreen;
  21. }
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. CheckForIllegalCrossThreadCalls = false;
  25. FormBorderStyle = FormBorderStyle.FixedSingle;
  26. InitDB = new Thread(ConnectDB);
  27. //添加监控事件
  28. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  29. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  30. stw.StartPosition = FormStartPosition.CenterScreen;
  31. stw.ShowDialog();
  32. List<string> CacheInf = new List<string>();
  33. dt = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address from master", "select");
  34. Master.DataSource = dt;
  35. Master.DisplayMember = "ma_user";
  36. Master.ValueMember = "ma_user";
  37. try
  38. {
  39. XmlReader myReader = XmlReader.Create(CachePath);
  40. while (myReader.Read())
  41. {
  42. if (myReader.NodeType == XmlNodeType.Text)
  43. CacheInf.Add(myReader.Value);
  44. }
  45. myReader.Close();
  46. string[] Info = CacheInf.ToArray();
  47. FolderPath.Text = Info[0];
  48. BackUpFolderPath.Text = Info[1];
  49. Source.Text = Info[2];
  50. Master.Text = Info[3];
  51. AutoStart.Checked = (Info[4] == "True" ? true : false);
  52. }
  53. catch (Exception) { }
  54. StartWatch.PerformClick();
  55. }
  56. private void ConnectDB()
  57. {
  58. dh = new DataHelper();
  59. }
  60. private void StartWatch_Click(object sender, EventArgs e)
  61. {
  62. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  63. {
  64. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  65. return;
  66. }
  67. else
  68. {
  69. if (!Directory.Exists(FolderPath.Text))
  70. {
  71. OperateResult.AppendText("监控文件夹不存在\n");
  72. return;
  73. }
  74. if (!Directory.Exists(BackUpFolderPath.Text))
  75. {
  76. OperateResult.AppendText("备份文件夹不存在\n");
  77. return;
  78. }
  79. }
  80. if (FolderPath.Text == BackUpFolderPath.Text)
  81. {
  82. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  83. return;
  84. }
  85. for (int i = 0; i < dt.Rows.Count; i++)
  86. {
  87. if (Master.Text == dt.Rows[i]["ma_user"].ToString())
  88. {
  89. DataHelper.DBConnectionString = "Data Source=" + dt.Rows[i]["ma_address"] + ";User ID=" + dt.Rows[i]["ma_user"] + ";PassWord=" + dt.Rows[i]["ms_pwd"]; ;
  90. dh = new DataHelper();
  91. }
  92. }
  93. if (!dh.CheckExist("source", "sc_code='" + Source.Text + "' and sc_statuscode='AUDITED'"))
  94. {
  95. OperateResult.AppendText("岗位资源错误或者未审核\n");
  96. return;
  97. }
  98. XmlWatcher.Path = FolderPath.Text;
  99. XmlWatcher.Filter = "*.xml";
  100. XmlWatcher.EnableRaisingEvents = true;
  101. try
  102. {
  103. File.Delete(CachePath);
  104. }
  105. catch (Exception) { }
  106. XmlDocument xmlDoc = new XmlDocument();
  107. //创建类型声明节点
  108. XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
  109. xmlDoc.AppendChild(node);
  110. //创建根节点
  111. XmlElement xeRoot = xmlDoc.CreateElement("CacheInf");
  112. xmlDoc.AppendChild(xeRoot);
  113. CreateNode(xmlDoc, xeRoot, "FolderPath", FolderPath.Text);
  114. CreateNode(xmlDoc, xeRoot, "BackUpFolderPath", BackUpFolderPath.Text);
  115. CreateNode(xmlDoc, xeRoot, "Source", Source.Text);
  116. CreateNode(xmlDoc, xeRoot, "Master", Master.Text);
  117. CreateNode(xmlDoc, xeRoot, "AutoStart", AutoStart.Checked.ToString());
  118. xmlDoc.Save(CachePath);
  119. Source.Enabled = false;
  120. StartWatch.Enabled = false;
  121. ChooseFolder.Enabled = false;
  122. ChooseBackUpFolder.Enabled = false;
  123. StopWatch.Enabled = true;
  124. OperateResult.AppendText("开始执行监控\n");
  125. }
  126. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  127. {
  128. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  129. node.InnerText = value;
  130. parentNode.AppendChild(node);
  131. }
  132. public void SetAutoRun()
  133. {
  134. if (AutoStart.Checked) //设置开机自启动
  135. {
  136. string path = Application.ExecutablePath;
  137. RegistryKey rk = Registry.LocalMachine;
  138. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  139. rk2.SetValue("UAS_XML解析器.exe", path);
  140. rk2.Close();
  141. rk.Close();
  142. }
  143. else //取消开机自启动
  144. {
  145. string path = Application.ExecutablePath;
  146. RegistryKey rk = Registry.LocalMachine;
  147. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  148. rk2.DeleteValue("UAS_XML解析器.exe", false);
  149. rk2.Close();
  150. rk.Close();
  151. }
  152. }
  153. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  154. {
  155. while (true)
  156. {
  157. try
  158. {
  159. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  160. {
  161. if (stream != null)
  162. break;
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. Console.WriteLine(ex.Message);
  168. }
  169. }
  170. string testDate = "";
  171. string testTime = "";
  172. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  173. //获取文件名的序列号,如SA123456.xml
  174. string sncode = e.Name.Split('.')[0];
  175. //获取序列号ID最大的工单号
  176. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "' order by ms_id desc").ToString();
  177. OperateResult.AppendText("读取文件" + e.Name + "\n");
  178. //获取采集的项目名称
  179. List<string> name = new List<string>();
  180. //获取采集项目的结果
  181. List<string> result = new List<string>();
  182. int name_or_result = 0;
  183. while (myReader.Read())
  184. {
  185. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
  186. {
  187. testDate = myReader.GetAttribute(1);
  188. testTime = myReader.GetAttribute(0);
  189. }
  190. if (myReader.NodeType == XmlNodeType.Text)
  191. {
  192. if (name_or_result % 2 == 0)
  193. {
  194. name.Add(myReader.Value);
  195. name_or_result++;
  196. }
  197. else
  198. {
  199. result.Add(myReader.Value);
  200. name_or_result++;
  201. }
  202. }
  203. }
  204. string date = testDate + " " + testTime;
  205. string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
  206. sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
  207. sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + Source.Text + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
  208. sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + date + "','YYYY/MM/DD hh24:mi:ss'))";
  209. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  210. myReader.Close();
  211. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  212. if (file.Exists)
  213. {
  214. try
  215. {
  216. for (int i = 1; i <= 20; i++)
  217. {
  218. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  219. {
  220. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  221. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  222. break;
  223. }
  224. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  225. {
  226. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  227. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  228. break;
  229. }
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. OperateResult.AppendText(e.Name + ex.Message + "\n");
  235. }
  236. }
  237. }
  238. private void StopWatch_Click(object sender, EventArgs e)
  239. {
  240. XmlWatcher.EnableRaisingEvents = false;
  241. Source.Enabled = true;
  242. StartWatch.Enabled = true;
  243. ChooseFolder.Enabled = true;
  244. ChooseBackUpFolder.Enabled = true;
  245. StopWatch.Enabled = false;
  246. OperateResult.AppendText("停止执行监控\n");
  247. }
  248. private void Clean_Click(object sender, EventArgs e)
  249. {
  250. OperateResult.Clear();
  251. }
  252. private void ChooseFolder_Click(object sender, EventArgs e)
  253. {
  254. FolderBrowserDialog folder = new FolderBrowserDialog();
  255. folder.Description = "选择监控文件夹";
  256. DialogResult result = folder.ShowDialog();
  257. if (result == DialogResult.OK)
  258. {
  259. FolderPath.Text = folder.SelectedPath;
  260. }
  261. }
  262. private void ReadNodeFromXML(string FileName)
  263. {
  264. }
  265. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  266. {
  267. FolderBrowserDialog folder = new FolderBrowserDialog();
  268. folder.Description = "选择备份文件夹";
  269. DialogResult result = folder.ShowDialog();
  270. if (result == DialogResult.OK)
  271. {
  272. BackUpFolderPath.Text = folder.SelectedPath;
  273. }
  274. }
  275. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  276. {
  277. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  278. if (ExitConfirm != "Yes")
  279. {
  280. WindowState = FormWindowState.Minimized;
  281. e.Cancel = true;
  282. }
  283. }
  284. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  285. {
  286. SetAutoRun();
  287. }
  288. }
  289. }