AutoAnalysisXLSJC.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. using System.Text;
  10. using System.Media;
  11. using System.Linq;
  12. using NPOI.SS.Formula.Functions;
  13. using System.Windows.Media.TextFormatting;
  14. using UMESDLLService;
  15. namespace FileWatcher
  16. {
  17. public partial class AutoAnalysisXLSJC : Form
  18. {
  19. public static DataHelper dh;
  20. DataTable dt;
  21. DataTable DB;
  22. /// <summary>
  23. /// 用户编号
  24. /// </summary>
  25. string iusercode;
  26. /// <summary>
  27. /// 岗位资源
  28. /// </summary>
  29. string isource;
  30. Tip tipform;
  31. Thread InitDB;
  32. /// <summary>
  33. /// 线别
  34. /// </summary>
  35. string ilinecode;
  36. /// <summary>
  37. /// 不良代码组
  38. /// </summary>
  39. string ibadgroup;
  40. /// <summary>
  41. /// 当前工序
  42. /// </summary>
  43. string istepcode;
  44. StringBuilder sql = new StringBuilder();
  45. /// <summary>
  46. /// 缓存的文件
  47. /// </summary>
  48. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  49. /// <summary>
  50. /// 缓存的文件夹
  51. /// </summary>
  52. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  53. /// <summary>
  54. /// 需要解析的文件名
  55. /// </summary>
  56. List<string> _FileName = new List<string>();
  57. ftpOperater ftp = new ftpOperater();
  58. string imaster;
  59. public AutoAnalysisXLSJC()
  60. {
  61. tipform = new Tip();
  62. InitializeComponent();
  63. StartPosition = FormStartPosition.CenterScreen;
  64. }
  65. public AutoAnalysisXLSJC(string iUserName, string iSource, string iMaster)
  66. {
  67. tipform = new Tip();
  68. InitializeComponent();
  69. iusercode = iUserName;
  70. isource = iSource.ToUpper();
  71. imaster = iMaster;
  72. StartPosition = FormStartPosition.CenterScreen;
  73. }
  74. string fieltype = "";
  75. string ftpulr = "";
  76. private void Form1_Load(object sender, EventArgs e)
  77. {
  78. tipform.Show();
  79. CheckForIllegalCrossThreadCalls = false;
  80. //FormBorderStyle = FormBorderStyle.FixedSingle;
  81. InitDB = new Thread(ConnectDB);
  82. //添加监控事件
  83. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  84. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  85. stw.StartPosition = FormStartPosition.CenterScreen;
  86. stw.ShowDialog();
  87. List<string> CacheInf = new List<string>();
  88. //获取缓存信息
  89. try
  90. {
  91. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  92. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  93. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  94. Device.Text = BaseUtil.GetCacheData("Device").ToString();
  95. }
  96. catch (Exception ex) { MessageBox.Show(ex.Message); }
  97. StartWatch.PerformClick();
  98. //bool hide = true;
  99. //if (hide)
  100. //{
  101. // this.WindowState = FormWindowState.Minimized;
  102. // this.ShowInTaskbar = false;
  103. //}
  104. }
  105. private void ConnectDB()
  106. {
  107. dh = new DataHelper();
  108. }
  109. private void StartWatch_Click(object sender, EventArgs e)
  110. {
  111. if (FolderPath.Text == "")
  112. {
  113. OperateResult.AppendText("请选择监控文件夹\n");
  114. return;
  115. }
  116. else
  117. {
  118. if (!Directory.Exists(FolderPath.Text))
  119. {
  120. OperateResult.AppendText("监控文件夹不存在\n");
  121. return;
  122. }
  123. }
  124. XmlWatcher.Path = FolderPath.Text;
  125. XmlWatcher.Filter = "*.*";
  126. XmlWatcher.EnableRaisingEvents = true;
  127. //设置缓存数据
  128. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  129. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  130. BaseUtil.SetCacheData("Master", Master.Text);
  131. BaseUtil.SetCacheData("Device", Device.Text);
  132. //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  133. Timer.Interval = 1000 * int.Parse(TimerInter.Text.ToString());
  134. Timer.Start();
  135. //设置按钮不可点击
  136. StartWatch.Enabled = false;
  137. ChooseFolder.Enabled = false;
  138. Master.Enabled = false;
  139. StopWatch.Enabled = true;
  140. OperateResult.AppendText("开始执行监控\n");
  141. //this.WindowState = FormWindowState.Minimized;
  142. //this.ShowInTaskbar = false;
  143. }
  144. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  145. {
  146. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  147. node.InnerText = value;
  148. parentNode.AppendChild(node);
  149. }
  150. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  151. {
  152. string[] str = new string[] { "xls" };
  153. for (int i = 0; i < str.Length; i++)
  154. {
  155. if (e.FullPath.ToUpper().EndsWith(str[i].ToUpper()))
  156. {
  157. TxtHandleProcess(e.FullPath);
  158. }
  159. }
  160. }
  161. string nextLine;
  162. private void TxtHandleProcess(string FileName)
  163. {
  164. List<string> badcode = new List<string>();
  165. List<string> badlocation = new List<string>();
  166. List<string> badprod = new List<string>();
  167. try
  168. {
  169. string filename = FileName;
  170. string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\"));
  171. string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
  172. string filetype = FileName.Substring(FileName.LastIndexOf(".") + 1);
  173. {
  174. try
  175. {
  176. string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  177. int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class,STD_RESCODE,std_stepcode,STD_TESTRESULT,std_class)select STEPTESTDETAIL_seq.nextval,'" + SN + "','',sysdate,'" + SN + "','" + isource + "','" + Device.Text + "','','http://172.16.51.3:8099/ftp" + ftppath + SN + ".xls" +"' from dual", "insert").ToString());
  178. if (num > 0)
  179. {
  180. ftp.UpLoadFile(folderpath, SN, ftppath, "");
  181. OperateResult.AppendText("序列号:" + SN + "上传成功\n");
  182. string Backupfilename = BackUpFolderPath.Text + @"\" + SN;
  183. if (!Directory.Exists(BackUpFolderPath.Text))
  184. {
  185. Directory.CreateDirectory(BackUpFolderPath.Text);
  186. }
  187. if (!deleteFile.Checked)
  188. {
  189. if (File.Exists(Backupfilename))
  190. {
  191. File.Delete(Backupfilename);
  192. }
  193. File.Move(FileName, Backupfilename);
  194. }
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. OperateResult.AppendText(ex.Message + ex.StackTrace + "\n");
  200. }
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. OperateResult.AppendText("解析失败:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n" + e.Message + "\n");
  206. }
  207. }
  208. private void StopWatch_Click(object sender, EventArgs e)
  209. {
  210. XmlWatcher.EnableRaisingEvents = false;
  211. StartWatch.Enabled = true;
  212. Master.Enabled = true;
  213. ChooseFolder.Enabled = true;
  214. StopWatch.Enabled = false;
  215. OperateResult.AppendText("停止执行监控\n");
  216. }
  217. private void Clean_Click(object sender, EventArgs e)
  218. {
  219. OperateResult.Clear();
  220. }
  221. private void ChooseFolder_Click(object sender, EventArgs e)
  222. {
  223. FolderBrowserDialog folder = new FolderBrowserDialog();
  224. folder.Description = "选择监控文件夹";
  225. DialogResult result = folder.ShowDialog();
  226. if (result == DialogResult.OK)
  227. {
  228. FolderPath.Text = folder.SelectedPath;
  229. }
  230. }
  231. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  232. {
  233. FolderBrowserDialog folder = new FolderBrowserDialog();
  234. folder.Description = "选择备份文件夹";
  235. DialogResult result = folder.ShowDialog();
  236. if (result == DialogResult.OK)
  237. {
  238. BackUpFolderPath.Text = folder.SelectedPath;
  239. }
  240. }
  241. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  242. {
  243. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  244. if (ExitConfirm != "Yes")
  245. {
  246. WindowState = FormWindowState.Minimized;
  247. e.Cancel = true;
  248. }
  249. }
  250. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  251. {
  252. SetAutoRun();
  253. }
  254. private void SetAutoRun()
  255. {
  256. if (AutoStart.Checked) //设置开机自启动
  257. {
  258. string path = Application.ExecutablePath;
  259. RegistryKey rk = Registry.LocalMachine;
  260. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  261. rk2.SetValue("UAS自动过站解析器.exe", path);
  262. rk2.Close();
  263. rk.Close();
  264. }
  265. else //取消开机自启动
  266. {
  267. string path = Application.ExecutablePath;
  268. RegistryKey rk = Registry.LocalMachine;
  269. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  270. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  271. rk2.Close();
  272. rk.Close();
  273. }
  274. }
  275. private void BatchAnalysus_Click(object sender, EventArgs e)
  276. {
  277. //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
  278. //foreach (var Xml in AllXmls)
  279. //{
  280. // TxtHandleProcess(Xml);
  281. //}
  282. }
  283. private void Timer_Tick(object sender, EventArgs e)
  284. {
  285. if (deleteFile.Checked)
  286. {
  287. //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".xls") || s.EndsWith(".ini"));
  288. //foreach (var Xml in AllXmls)
  289. //{
  290. // File.Delete(Xml.ToString());
  291. //}
  292. }
  293. }
  294. private void CleanFolderPath_Click(object sender, EventArgs e)
  295. {
  296. FolderPath.Text = "";
  297. }
  298. private void CleanBackUpFolderPath_Click(object sender, EventArgs e)
  299. {
  300. BackUpFolderPath.Text = "";
  301. }
  302. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  303. {
  304. if (WindowState == FormWindowState.Minimized)
  305. {
  306. //WindowState = FormWindowState.Normal;
  307. //ShowInTaskbar = true;
  308. }
  309. }
  310. private void AutoAnalysisXmlByStep_Deactivate(object sender, EventArgs e)
  311. {
  312. if (WindowState == FormWindowState.Minimized)
  313. {
  314. //ShowInTaskbar = false;
  315. }
  316. }
  317. private void BatchAnalysus_Click_1(object sender, EventArgs e)
  318. {
  319. var AllXmls = Directory.GetFiles(FolderPath.Text, "*.xls");
  320. foreach (var Xml in AllXmls)
  321. {
  322. TxtHandleProcess(Xml);
  323. }
  324. }
  325. }
  326. }