using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using System.Xml; namespace UAS_AutoPass.ToolClass { class BaseUtil { public static void CreateXml(string iSN, string iTestResult) { //创建XML文档 FileStream fcaches = new FileStream(iSN + ".xml", FileMode.OpenOrCreate, FileAccess.ReadWrite); fcaches.Close(); FileInfo info = new FileInfo(iSN + ".xml"); if (info.Length == 0) { XmlDocument doc = new XmlDocument(); //创建类型声明节点 XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", ""); doc.AppendChild(node); //创建根节点 XmlElement xeRoot = doc.CreateElement("cacheInfo"); doc.AppendChild(xeRoot); doc.Save(iSN + ".xml"); } } public static object GetCacheData(string ParamName) { try { object returnData = null; //根据地址读取xml文件 XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); //忽略文档里面的注释 settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings); doc.Load(reader); //先得到根节点 XmlNode rootNode = doc.SelectSingleNode("cacheInfo"); //再由根节点去找制定的节点 XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { //找到了这个节点名字 if (node.Name == ParamName) { //返回节点的内容 switch (((XmlElement)node).GetAttribute("Type")) { case "System.String": returnData = node.InnerText; break; case "System.Int32": returnData = int.Parse(node.InnerText); break; case "System.Boolean": returnData = node.InnerText == "True" ? true : false; break; default: break; } break; } } //关闭reader reader.Close(); if (returnData == null) return ""; else return returnData; } catch (Exception e) { Console.WriteLine(e.Message + e.StackTrace); return ""; } } public static void SetCacheData(string ParamName, object Value) { try { //根据地址读取xml文件 XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); //忽略文档里面的注释 settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(AutoAnalysisXml.CachePath, settings); doc.Load(reader); //先得到根节点 XmlNode rootNode = doc.SelectSingleNode("cacheInfo"); //再由根节点去找制定的节点 XmlNodeList nodeList = rootNode.ChildNodes; bool flag = false; foreach (XmlNode node in nodeList) { //找到了这个节点名字 if (node.Name == ParamName) { //就直接赋值 node.InnerText = Value.ToString(); flag = true; } } //如果没有该节点,就创建节点保存结果 if (!flag) { //创建节点 XmlElement newNode = doc.CreateElement(ParamName); XmlAttribute attr = doc.CreateAttribute("Type"); attr.InnerText = Value.GetType().ToString(); newNode.InnerText = Value.ToString(); newNode.SetAttributeNode(attr); //讲新建的节点挂到根节点上 rootNode.AppendChild(newNode); } //关闭Reader reader.Close(); doc.Save(AutoAnalysisXml.CachePath); } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); } } //播放音频文件 public static void PlaySound(string FileName) { //要加载COM组件:Microsoft speech object Library if (!System.IO.File.Exists(FileName)) { return; } SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass(); SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass(); spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true); SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream; pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename); spFs.Close(); } public static void GetWriteInfo(string FilePath, out Dictionary Dic) { Dic = new Dictionary(); string txt = ""; StreamReader sr = new StreamReader(FilePath); while (!sr.EndOfStream) { string str = sr.ReadLine(); txt += str + "\n"; } sr.Close(); sr.Dispose(); Dic.Add("atd_sncode", FilePath.Substring(FilePath.LastIndexOf("\\") + 1).Replace(".txt", "")); Dic.Add("atd_software", Regex.Match(txt, "Program Name,\\S+").Value.Replace("Program Name,", "")); Dic.Add("atd_pot", Regex.Match(txt, "Board Segment,\\S+").Value.Replace("Board Segment,", "")); Dic.Add("atd_size", Regex.Match(txt, "Baord Size \\(L x W\\) \\[mm\\],\\S+").Value.Replace("Baord Size (L x W) [mm],", "")); Dic.Add("atd_pot1set", Regex.Match(txt, "Pot-1 Set Temp. \\[deg],\\S+").Value.Replace("Pot-1 Set Temp. [deg],", "")); Dic.Add("atd_pot2set", Regex.Match(txt, "Pot-2 Set Temp. \\[deg],\\S+").Value.Replace("Pot-2 Set Temp. [deg],", "")); Dic.Add("atd_pot1avgtemp", Regex.Match(txt, "Pot-1 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-1 Avg. Temp. [deg],", "")); Dic.Add("atd_pot2avgtemp", Regex.Match(txt, "Pot-2 Avg. Temp. \\[deg],\\S+").Value.Replace("Pot-2 Avg. Temp. [deg],", "")); Dic.Add("atd_boardtime", Regex.Match(txt, "Machine Duration \\[s],\\S+").Value.Replace("Machine Duration [s],", "")); //开始时间 MatchCollection starttime = Regex.Matches(txt, "Start TimeStamp,\\S+ \\S+"); for (int i = 0; i < starttime.Count; i++) { switch (i) { case 0: Dic.Add("atd_boardstarttime", starttime[i].Value.Replace("Start TimeStamp,", "")); break; case 1: Dic.Add("atd_flstarttime", starttime[i].Value.Replace("Start TimeStamp,", "")); break; case 2: Dic.Add("atd_phstarttime", starttime[i].Value.Replace("Start TimeStamp,", "")); break; case 3: Dic.Add("atd_sostarttime", starttime[i].Value.Replace("Start TimeStamp,", "")); break; default: break; } } MatchCollection endtime = Regex.Matches(txt, "End TimeStamp,\\S+ \\S+"); for (int i = 0; i < starttime.Count; i++) { switch (i) { case 0: Dic.Add("atd_boardendtime", endtime[i].Value.Replace("End TimeStamp,", "")); break; case 1: Dic.Add("atd_flendtime", endtime[i].Value.Replace("End TimeStamp,", "")); break; case 2: Dic.Add("atd_phendtime", endtime[i].Value.Replace("End TimeStamp,", "")); break; case 3: Dic.Add("atd_soendtime", endtime[i].Value.Replace("End TimeStamp,", "")); break; default: break; } } MatchCollection duration = Regex.Matches(txt, "Total Zone Duration \\[s],\\S+"); for (int i = 0; i < starttime.Count; i++) { switch (i) { case 0: Dic.Add("atd_fltime", duration[i].Value.Replace("Total Zone Duration [s],", "")); break; case 1: Dic.Add("atd_phtime", duration[i].Value.Replace("Total Zone Duration [s],", "")); break; case 2: Dic.Add("atd_sotime", duration[i].Value.Replace("Total Zone Duration [s],", "")); break; default: break; } } } } }