using Microsoft.Win32; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Threading; using System.Windows.Forms; using System.Xml; using System.Text; using System.Media; using System.Linq; namespace FileWatcher { public partial class AutoAnalysisXmlByStep : Form { public static DataHelper dh; DataTable dt; DataTable DB; /// /// 用户编号 /// string iusercode; /// /// 岗位资源 /// string isource; Tip tipform; Thread InitDB; /// /// 线别 /// string ilinecode; /// /// 不良代码组 /// string ibadgroup; /// /// 当前工序 /// string istepcode; StringBuilder sql = new StringBuilder(); /// /// 缓存的文件 /// public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml"; /// /// 缓存的文件夹 /// public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/"; /// /// 需要解析的文件名 /// List _FileName = new List(); ftpOperater ftp; string imaster; public AutoAnalysisXmlByStep() { tipform = new Tip(); InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } public AutoAnalysisXmlByStep(string iUserName, string iSource, string iMaster) { tipform = new Tip(); InitializeComponent(); iusercode = iUserName; isource = iSource.ToUpper(); imaster = iMaster; StartPosition = FormStartPosition.CenterScreen; } string fieltype = ""; string ftpulr = ""; private void Form1_Load(object sender, EventArgs e) { tipform.Show(); CheckForIllegalCrossThreadCalls = false; FormBorderStyle = FormBorderStyle.FixedSingle; InitDB = new Thread(ConnectDB); //添加监控事件 XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created); SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序"); stw.StartPosition = FormStartPosition.CenterScreen; stw.ShowDialog(); List CacheInf = new List(); ftpulr = dh.getFieldDataByCondition("configs", "data", "code='UploadFTP' and caller='MESSetting'").ToString(); fieltype = dh.getFieldDataByCondition("configs", "data", "code='UploadFile' and caller='MESSetting'").ToString(); string[] FTPInf = ftpulr.Split('#'); ftp = new ftpOperater(FTPInf[0], FTPInf[1], FTPInf[2]); //获取缓存信息 try { //FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString(); //BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString(); //Master.Text = BaseUtil.GetCacheData("Master").ToString(); //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void ConnectDB() { dh = new DataHelper(); } private void StartWatch_Click(object sender, EventArgs e) { if (FolderPath.Text == "") { OperateResult.AppendText("请选择监控文件夹\n"); return; } else { if (!Directory.Exists(FolderPath.Text)) { OperateResult.AppendText("监控文件夹不存在\n"); return; } } XmlWatcher.Path = FolderPath.Text; XmlWatcher.Filter = "*.*"; XmlWatcher.EnableRaisingEvents = true; //设置缓存数据 //BaseUtil.SetCacheData("FolderPath", FolderPath.Text); //BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text); //BaseUtil.SetCacheData("Master", Master.Text); //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked); Timer.Interval = 1000 * 10; Timer.Start(); //设置按钮不可点击 StartWatch.Enabled = false; ChooseFolder.Enabled = false; Master.Enabled = false; StopWatch.Enabled = true; OperateResult.AppendText("开始执行监控\n"); } public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentNode.AppendChild(node); } private void XmlWatcher_Created(object sender, FileSystemEventArgs e) { string[] str = fieltype.Split('#'); for (int i = 0; i < str.Length; i++) { if (e.FullPath.EndsWith(str[i])) { TxtHandleProcess(e.FullPath); } } } private void TxtHandleProcess(string FileName) { List badcode = new List(); List badlocation = new List(); List badprod = new List(); try { string filename = FileName; string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\")); string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1); { try { string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"; ftp.UpLoadFile(folderpath, SN, ftppath, ""); int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class,STD_RESCODE,std_stepcode)select STEPTESTDETAIL_seq.nextval,'" + SN + "','',sysdate,'" + ftpulr.Split('#')[0] + imaster + "/" + ftppath + SN + ".txt" + "','" + isource + "','' from dual", "insert").ToString()); if (num > 0) { OperateResult.AppendText("序列号:" + SN + "上传成功\n"); //File.Delete(FileName); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } catch (Exception e) { OperateResult.AppendText("解析失败:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n" + e.Message + "\n"); } } private void StopWatch_Click(object sender, EventArgs e) { XmlWatcher.EnableRaisingEvents = false; StartWatch.Enabled = true; Master.Enabled = true; ChooseFolder.Enabled = true; StopWatch.Enabled = false; OperateResult.AppendText("停止执行监控\n"); } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } private void ChooseFolder_Click(object sender, EventArgs e) { FolderBrowserDialog folder = new FolderBrowserDialog(); folder.Description = "选择监控文件夹"; DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK) { FolderPath.Text = folder.SelectedPath; } } private void ChooseBackUpFolder_Click(object sender, EventArgs e) { FolderBrowserDialog folder = new FolderBrowserDialog(); folder.Description = "选择备份文件夹"; DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK) { } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (ExitConfirm != "Yes") { WindowState = FormWindowState.Minimized; e.Cancel = true; } } private void AutoStart_CheckedChanged(object sender, EventArgs e) { SetAutoRun(); } private void SetAutoRun() { if (AutoStart.Checked) //设置开机自启动 { string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("UAS自动过站解析器.exe", path); rk2.Close(); rk.Close(); } else //取消开机自启动 { string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("UAS自动过站解析器.exe", false); rk2.Close(); rk.Close(); } } private void BatchAnalysus_Click(object sender, EventArgs e) { //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt"); //foreach (var Xml in AllXmls) //{ // TxtHandleProcess(Xml); //} } private void Timer_Tick(object sender, EventArgs e) { //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".ini")); //foreach (var Xml in AllXmls) //{ // TxtHandleProcess(Xml); //} } } }