Form1.cs 11 KB

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