123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- 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 UAS_AutoPass.ToolClass;
- namespace UAS_AutoPass
- {
- public partial class AutoAnalysisXml : Form
- {
- DataHelper dh;
- DataTable dt;
- Thread InitDB;
- /// <summary>
- /// 缓存的文件
- /// </summary>
- string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
- /// <summary>
- /// 缓存的文件夹
- /// </summary>
- string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
- public AutoAnalysisXml()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- 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<string> CacheInf = new List<string>();
- dt = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address from master", "select");
- Master.DataSource = dt;
- Master.DisplayMember = "ma_user";
- Master.ValueMember = "ma_user";
- try
- {
- if (!Directory.Exists(CachePathFolder))
- Directory.CreateDirectory(CachePathFolder);
- XmlReader myReader = XmlReader.Create(CachePath);
- while (myReader.Read())
- {
- if (myReader.NodeType == XmlNodeType.Text)
- CacheInf.Add(myReader.Value);
- }
- myReader.Close();
- string[] Info = CacheInf.ToArray();
- FolderPath.Text = Info[0];
- BackUpFolderPath.Text = Info[1];
- Source.Text = Info[2];
- Master.Text = Info[3];
- AutoStart.Checked = (Info[4] == "True" ? true : false);
- }
- catch (Exception) { }
- StartWatch.PerformClick();
- }
- private void ConnectDB()
- {
- dh = new DataHelper();
- }
- private void StartWatch_Click(object sender, EventArgs e)
- {
- if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
- {
- OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
- return;
- }
- else
- {
- if (!Directory.Exists(FolderPath.Text))
- {
- OperateResult.AppendText("监控文件夹不存在\n");
- return;
- }
- if (!Directory.Exists(BackUpFolderPath.Text))
- {
- OperateResult.AppendText("备份文件夹不存在\n");
- return;
- }
- }
- if (FolderPath.Text == BackUpFolderPath.Text)
- {
- OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
- return;
- }
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- if (Master.Text == dt.Rows[i]["ma_user"].ToString())
- {
- DataHelper.DBConnectionString = "Data Source=" + dt.Rows[i]["ma_address"] + ";User ID=" + dt.Rows[i]["ma_user"] + ";PassWord=" + dt.Rows[i]["ms_pwd"]; ;
- dh = new DataHelper();
- }
- }
- if (!dh.CheckExist("source", "sc_code='" + Source.Text + "' and sc_statuscode='AUDITED'"))
- {
- OperateResult.AppendText("岗位资源错误或者未审核\n");
- return;
- }
- XmlWatcher.Path = FolderPath.Text;
- XmlWatcher.Filter = "*.xml";
- XmlWatcher.EnableRaisingEvents = true;
- try
- {
- File.Delete(CachePath);
- }
- catch (Exception) { }
- XmlDocument xmlDoc = new XmlDocument();
- //创建类型声明节点
- XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
- xmlDoc.AppendChild(node);
- //创建根节点
- XmlElement xeRoot = xmlDoc.CreateElement("CacheInf");
- xmlDoc.AppendChild(xeRoot);
- CreateNode(xmlDoc, xeRoot, "FolderPath", FolderPath.Text);
- CreateNode(xmlDoc, xeRoot, "BackUpFolderPath", BackUpFolderPath.Text);
- CreateNode(xmlDoc, xeRoot, "Source", Source.Text);
- CreateNode(xmlDoc, xeRoot, "Master", Master.Text);
- CreateNode(xmlDoc, xeRoot, "AutoStart", AutoStart.Checked.ToString());
- xmlDoc.Save(CachePath);
- Source.Enabled = false;
- StartWatch.Enabled = false;
- ChooseFolder.Enabled = false;
- ChooseBackUpFolder.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);
- }
- public 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_XML解析器.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_XML解析器.exe", false);
- rk2.Close();
- rk.Close();
- }
- }
- private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
- {
- while (true)
- {
- try
- {
- using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- if (stream != null)
- break;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- string testDate = "";
- string testTime = "";
- XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
- //获取文件名的序列号,如SA123456.xml
- string sncode = e.Name.Split('.')[0];
- //获取序列号ID最大的工单号
- string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "' order by ms_id desc").ToString();
- OperateResult.AppendText("读取文件" + e.Name + "\n");
- //获取采集的项目名称
- List<string> name = new List<string>();
- //获取采集项目的结果
- List<string> result = new List<string>();
- int name_or_result = 0;
- while (myReader.Read())
- {
- if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
- {
- testDate = myReader.GetAttribute(1);
- testTime = myReader.GetAttribute(0);
- }
- if (myReader.NodeType == XmlNodeType.Text)
- {
- if (name_or_result % 2 == 0)
- {
- name.Add(myReader.Value);
- name_or_result++;
- }
- else
- {
- result.Add(myReader.Value);
- name_or_result++;
- }
- }
- }
- string date = testDate + " " + testTime;
- string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
- sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
- sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + Source.Text + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
- sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + date + "','YYYY/MM/DD hh24:mi:ss'))";
- dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
- myReader.Close();
- FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
- if (file.Exists)
- {
- try
- {
- for (int i = 1; i <= 20; i++)
- {
- if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
- {
- file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
- OperateResult.AppendText("成功解析文件" + e.Name + "\n");
- break;
- }
- else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
- {
- file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
- OperateResult.AppendText("成功解析文件" + e.Name + "\n");
- break;
- }
- }
- }
- catch (Exception ex)
- {
- OperateResult.AppendText(e.Name + ex.Message + "\n");
- }
- }
- }
- private void StopWatch_Click(object sender, EventArgs e)
- {
- XmlWatcher.EnableRaisingEvents = false;
- Source.Enabled = true;
- StartWatch.Enabled = true;
- ChooseFolder.Enabled = true;
- ChooseBackUpFolder.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 ReadNodeFromXML(string FileName)
- {
- }
- private void ChooseBackUpFolder_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog folder = new FolderBrowserDialog();
- folder.Description = "选择备份文件夹";
- DialogResult result = folder.ShowDialog();
- if (result == DialogResult.OK)
- {
- BackUpFolderPath.Text = folder.SelectedPath;
- }
- }
- 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();
- }
- }
- }
|