Form1.cs 12 KB

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