Form1.cs 12 KB

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