Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. OperateResult.AppendText("开始执行监控\n");
  113. }
  114. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  115. {
  116. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  117. node.InnerText = value;
  118. parentNode.AppendChild(node);
  119. }
  120. public void SetAutoRun()
  121. {
  122. if (AutoStart.Checked) //设置开机自启动
  123. {
  124. string path = Application.ExecutablePath;
  125. RegistryKey rk = Registry.LocalMachine;
  126. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  127. rk2.SetValue("UAS_XML解析器.exe", path);
  128. rk2.Close();
  129. rk.Close();
  130. }
  131. else //取消开机自启动
  132. {
  133. string path = Application.ExecutablePath;
  134. RegistryKey rk = Registry.LocalMachine;
  135. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  136. rk2.DeleteValue("UAS_XML解析器.exe", false);
  137. rk2.Close();
  138. rk.Close();
  139. }
  140. }
  141. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  142. {
  143. while (true)
  144. {
  145. try
  146. {
  147. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  148. {
  149. if (stream != null)
  150. break;
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. Console.WriteLine(ex.Message);
  156. }
  157. }
  158. string testDate = "";
  159. string testTime = "";
  160. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  161. string sncode = e.Name.Split('.')[0];
  162. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "'").ToString();
  163. OperateResult.AppendText("读取文件" + e.Name + "\n");
  164. List<string> name = new List<string>();
  165. List<string> result = new List<string>();
  166. int name_or_result = 0;
  167. while (myReader.Read())
  168. {
  169. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
  170. {
  171. testDate = myReader.GetAttribute(1);
  172. testTime = myReader.GetAttribute(0);
  173. }
  174. if (myReader.NodeType == XmlNodeType.Text)
  175. {
  176. if (name_or_result % 2 == 0)
  177. {
  178. name.Add(myReader.Value);
  179. name_or_result++;
  180. }
  181. else
  182. {
  183. result.Add(myReader.Value);
  184. name_or_result++;
  185. }
  186. }
  187. }
  188. string date = testDate + " " + testTime;
  189. string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
  190. sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
  191. sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + Source.Text + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
  192. sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + date + "','YYYY/MM/DD hh24:mi:ss'))";
  193. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  194. myReader.Close();
  195. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  196. if (file.Exists)
  197. {
  198. try
  199. {
  200. for (int i = 1; i <= 20; i++)
  201. {
  202. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  203. {
  204. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  205. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  206. break;
  207. }
  208. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  209. {
  210. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  211. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  212. break;
  213. }
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. OperateResult.AppendText(e.Name + ex.Message + "\n");
  219. }
  220. }
  221. }
  222. private void StopWatch_Click(object sender, EventArgs e)
  223. {
  224. XmlWatcher.EnableRaisingEvents = false;
  225. Source.Enabled = true;
  226. StartWatch.Enabled = true;
  227. ChooseFolder.Enabled = true;
  228. ChooseBackUpFolder.Enabled = true;
  229. StopWatch.Enabled = false;
  230. OperateResult.AppendText("停止执行监控\n");
  231. }
  232. private void Clean_Click(object sender, EventArgs e)
  233. {
  234. OperateResult.Clear();
  235. }
  236. private void ChooseFolder_Click(object sender, EventArgs e)
  237. {
  238. FolderBrowserDialog folder = new FolderBrowserDialog();
  239. folder.Description = "选择监控文件夹";
  240. DialogResult result = folder.ShowDialog();
  241. if (result == DialogResult.OK)
  242. {
  243. FolderPath.Text = folder.SelectedPath;
  244. }
  245. }
  246. private void ReadNodeFromXML(string FileName)
  247. {
  248. }
  249. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  250. {
  251. FolderBrowserDialog folder = new FolderBrowserDialog();
  252. folder.Description = "选择备份文件夹";
  253. DialogResult result = folder.ShowDialog();
  254. if (result == DialogResult.OK)
  255. {
  256. BackUpFolderPath.Text = folder.SelectedPath;
  257. }
  258. }
  259. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  260. {
  261. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  262. if (ExitConfirm != "Yes")
  263. {
  264. WindowState = FormWindowState.Minimized;
  265. e.Cancel = true;
  266. }
  267. }
  268. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  269. {
  270. SetAutoRun();
  271. }
  272. }
  273. }