Beispiel #1
0
        /// <summary>
        /// 从单子节点 xml 获取Value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="xmlFileName"></param>
        /// <returns></returns>
        public static string GetSingleValueFromChildNode1(string key, string xmlFileName)
        {
            string      Rtn = "";
            XmlDocument doc = new XmlDocument();

            xmlFileName = AvoidXmlNotFound_DuringDebug(xmlFileName);

            try
            {
                doc.Load(xmlFileName);                     //加载Xml文件
                XmlElement rootElem = doc.DocumentElement; //获取根节点

                XmlNode node = rootElem.ChildNodes[0];
                if (node == null)
                {
                    return("");
                }

                XmlElement element = node as XmlElement;
                Rtn = element.GetAttribute(key);
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
            }

            return(Rtn);
        }
Beispiel #2
0
        public static void CreateMainSPF(List <string> contentBuilder, string FileNamePath)
        {
            try
            {
                // Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
                var    Filepath_Split = FileNamePath.Split(new char[] { '\\' });
                string ExeFullName    = Filepath_Split[Filepath_Split.Length - 1];
                string ExeName        = ExeFullName.Replace(".SPF", "");
                string FileFolder     = FileNamePath.TrimEnd(ExeFullName);

                if (!Directory.Exists(FileFolder))
                {
                    Directory.CreateDirectory(FileFolder);
                }

                File.WriteAllText(FileNamePath, String.Empty);
                //Create Stream to write to file.
                StreamWriter sWriter = new StreamWriter(FileNamePath);
                foreach (string cmd in contentBuilder)
                {
                    sWriter.WriteLine(cmd);
                }
                sWriter.Close();
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
                return;
            }
        }
Beispiel #3
0
        /// <summary>
        /// 程序运行结束后自我关闭,保证只有一个进程在执行。
        /// exe 文件修改后,需对应修改 bat 文件中的文件名
        /// CreateBat = true 有时会出现access denied 错误
        /// </summary>
        public static void Suicide(string ProcessName, string SuicideBatName = "Suicide.bat", bool CreateBat = false)
        {
            string dir         = Directory.GetCurrentDirectory() + "\\";
            string BatFilePath = string.Format("{0}{1}", dir, SuicideBatName);

            if (CreateBat)
            {
                ProcessName = ProcessName.TrimEnd(".exe");
                string Suicide_cmd = string.Format("taskkill /f /im {0}.exe", ProcessName);

                #region 写入 bat 文件

                try
                {
                    // Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
                    File.WriteAllText(BatFilePath, Suicide_cmd);
                }
                catch (Exception ex)
                {
                    SimpleLogHelper.Error(ex);
                    return;
                }

                #endregion
            }

            RunBat_byPath(BatFilePath);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="processPath">进程路径</param>
        /// <param name="processName"></param>
        /// <param name="batName"></param>
        /// <param name="CreateBat">CreateBat = true 有时会出现access denied 错误,建议一般用 false</param>
        public static void Restart(string processPath, string processName, string batName = "Restart.bat", bool CreateBat = false)
        {
            if (CreateBat)
            {
                string dir = Directory.GetCurrentDirectory() + "\\";

                processName = processName.TrimEnd(".exe");

                StringBuilder sb = new StringBuilder();

                sb.AppendLine(string.Format("taskkill /f /im {0}.exe", processName));

                sb.AppendLine(processPath);

                string batFilePath = string.Format("{0}{1}", dir, batName);

                #region 写入 bat 文件

                try
                {
                    // Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
                    File.WriteAllText(batFilePath, sb.ToString());
                }
                catch (Exception ex)
                {
                    SimpleLogHelper.Error(ex);
                    return;
                }

                #endregion
            }

            BatHelper.RunBat_sameDir(batName);
        }
Beispiel #5
0
        public static void Create_RestartBat(string processName, string batName = "Restart.bat")
        {
            string dir = Directory.GetCurrentDirectory() + "\\";

            processName = processName.TrimEnd(".exe");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("taskkill /f /im {0}.exe", processName));
            sb.AppendLine(string.Format("{0}{1}.exe", dir, processName)); // D:\mesline\WpfApplication1\bin\x86\Debug\
            string batFilePath = string.Format("{0}{1}", dir, batName);

            #region 写入 bat 文件

            try
            {
                // Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
                File.WriteAllText(batFilePath, sb.ToString());
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
                return;
            }

            #endregion
        }
Beispiel #6
0
        /// <summary>
        /// 用于小老化室温度显示,跟踪采集到的参数数值
        /// </summary>
        /// <param name="dic"></param>
        public static void Info <T1, T2>(Dictionary <T1, T2> dic)
        {
            int dic_Count = dic.Count;

            if (dic_Count == 0)
            {
                return;
            }

            StringBuilder sb              = new StringBuilder(0);
            string        temp            = "";
            int           signal_FirstRow = 0;

            // 第一行之外附加"     ",以便对齐nlog样式
            foreach (var dic_item in dic)
            {
                signal_FirstRow++;
                if (signal_FirstRow == 1)
                {
                    temp = string.Format("{0}:{1}", dic_item.Key, dic_item.Value);
                }
                else
                {
                    temp = string.Format("{0}{1}:{2}", "     ", dic_item.Key, dic_item.Value);
                }

                if (signal_FirstRow < dic_Count)
                {
                    sb.AppendLine(temp);
                }
                else
                {
                    sb.Append(temp);                     //去除 nlog 多余回行
                }
            }

            temp = sb.ToString();
            if (temp.IsNullOrEmpty())
            {
                return;
            }

            try
            {
                if (temp.ContainsEx(" not "))                 // 显示错误型日志
                {
                    nlogger.Log(LogLevel.Error, temp);
                    return;
                }
                nlogger.Log(LogLevel.Info, temp);
            }
            catch
            {
                SimpleLogHelper.Error(temp);
            }
        }
Beispiel #7
0
        public static void Error <T>(List <T> parameterValues)
        {
            int list_Count = parameterValues.Count;

            if (list_Count == 0)
            {
                return;
            }

            StringBuilder sb              = new StringBuilder(0);
            string        temp            = "";
            int           signal_FirstRow = 0;

            // 第一行之外附加"     ",以便对齐nlog样式
            foreach (var listItem in parameterValues)
            {
                signal_FirstRow++;
                if (signal_FirstRow == 1)
                {
                    temp = StringHelper.SafeToString(listItem);
                }
                else
                {
                    temp = string.Format("{0}{1}", "     ", StringHelper.SafeToString(listItem));
                }

                if (signal_FirstRow < list_Count)
                {
                    sb.AppendLine(temp);
                }
                else
                {
                    sb.Append(temp);                     //去除多余回行
                }
            }

            temp = sb.ToString();
            if (temp.IsNullOrEmpty())
            {
                return;
            }

            try
            {
                if (temp.ContainsEx(" not "))                 // 显示错误型日志
                {
                    nlogger.Log(LogLevel.Error, temp);
                    return;
                }
                nlogger.Log(LogLevel.Error, temp);
            }
            catch
            {
                SimpleLogHelper.Error(temp);
            }
        }
Beispiel #8
0
 public static void Info(string msg)
 {
     try
     {
         nlogger.Log(LogLevel.Info, msg);
     }
     catch
     {
         SimpleLogHelper.Error(msg);
     }
 }
Beispiel #9
0
 public static void Debug(string msg)
 {
     try
     {
         nlogger.Log(LogLevel.Debug, msg);
     }
     catch
     {
         SimpleLogHelper.Error(msg);
     }
 }
Beispiel #10
0
        /// <summary>
        /// 写入 value 到 CData
        /// </summary>
        /// <param name="key"></param>
        /// <param name="strValue"></param>
        /// <param name="xmlFileName"></param>
        /// <param name="nIndex">CDATA 在 childnode 中 的索引</param>
        /// <returns></returns>
        public static string WriteCData_inAppSettingsLikeXML(string key, string strValue, string xmlFileName, int nIndex = 0)
        {
            string Error_Msg = "";

            xmlFileName = AvoidXmlNotFound_DuringDebug(xmlFileName);

            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(xmlFileName);                     //加载 Xml 文件

                XmlElement rootElem = doc.DocumentElement; //获取根节点
                XmlElement node     = null;

                foreach (var xmlNode in rootElem.ChildNodes)
                {
                    node = xmlNode as XmlElement;
                    if (node == null)
                    {
                        continue;
                    }
                    if (node.NodeType == XmlNodeType.Element && node.GetAttribute("key").Trim() == key.Trim())
                    {
                        if (node.ChildNodes.Count > nIndex && node.ChildNodes[nIndex] != null && node.ChildNodes[nIndex].NodeType == XmlNodeType.CDATA)
                        {
                            node.ChildNodes[nIndex].Value = strValue;
                        }
                        else // 若不存在则新增 Cdata
                        {
                            XmlCDataSection m_cdata = doc.CreateCDataSection(strValue);
                            node.AppendChild(m_cdata);
                        }

                        doc.Save(xmlFileName);
                    }
                }
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
                return(ex.Message);
            }

            return(Error_Msg);
        }
Beispiel #11
0
        /// <summary>
        /// 从
        /// <add key="Camera2" name="C525"  >
        ///<![CDATA[usb#vid_058f&pid_a014&mi_00#7&1edadac0&0&0000]]>
        /// </add>
        /// 中读取 cdata 内容
        /// </summary>
        /// <param name="key"></param>
        /// <param name="xmlFileName"></param>
        /// <param name="nIndex">CDATA 在 childnode 中 的索引</param>
        /// <returns></returns>
        public static string GetValue_FromCData_inAppSettingsLikeXML(string key, string xmlFileName, int nIndex = 0)
        {
            string Rtn = "";

            xmlFileName = AvoidXmlNotFound_DuringDebug(xmlFileName);

            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(xmlFileName);    //加载 Xml 文件
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
                return("");
            }

            XmlElement rootElem = doc.DocumentElement;   //获取根节点
            XmlElement node     = null;

            foreach (var xmlNode in rootElem.ChildNodes)
            {
                node = xmlNode as XmlElement;
                if (node == null)
                {
                    continue;
                }

                if (node.NodeType == XmlNodeType.Element && node.GetAttribute("key").Trim() == key.Trim())
                {
                    if (node.ChildNodes.Count > nIndex && node.ChildNodes[nIndex] != null && node.ChildNodes[nIndex].NodeType == XmlNodeType.CDATA)
                    {
                        return(node.ChildNodes[nIndex].Value);
                    }
                }
            }

            return(Rtn);
        }
Beispiel #12
0
        /// <summary>
        /// 从类似于 AppSettings、名称为 ProcessName 的 xml,根据 add > key/value 获取节点信息;实现配置文件的 partial
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="AttributeName">
        /// 使用默认值 = app.config;
        /// 扩展为可使用自定义属性
        /// </param>
        /// <returns></returns>
        public static string GetValueLikeAppSettings(string key, string xmlFileName, string AttributeName = "value")
        {
            string Rtn = "";

            xmlFileName = AvoidXmlNotFound_DuringDebug(xmlFileName);

            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(xmlFileName);    //加载 Xml 文件
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
                return("");
            }

            XmlElement rootElem = doc.DocumentElement;   //获取根节点
            XmlElement node     = null;

            foreach (var xmlNode in rootElem.ChildNodes)
            {
                node = xmlNode as XmlElement;
                if (node == null)
                {
                    continue;
                }

                if (node.NodeType == XmlNodeType.Element && node.GetAttribute("key").Trim() == key.Trim())
                {
                    Rtn = node.GetAttribute(AttributeName);
                    return(Rtn);
                }
            }

            return(Rtn);
        }
Beispiel #13
0
        /// <summary>
        ///  从多子节点 xml 获取单个value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="childrootName"></param>
        /// <param name="xmlFileName"></param>
        /// <returns></returns>
        public static string GetSingleValue(string key, string childrootName, string xmlFileName)
        {
            string      Rtn = "";
            XmlDocument doc = new XmlDocument();

            xmlFileName = AvoidXmlNotFound_DuringDebug(xmlFileName);

            try
            {
                doc.Load(xmlFileName);                                                //加载Xml文件
                XmlElement rootElem = doc.DocumentElement;                            //获取根节点

                XmlNodeList rootNodes = rootElem.GetElementsByTagName(childrootName); //获取rootName子节点集合
                XmlElement  node      = rootNodes[0] as XmlElement;
                Rtn = node.GetAttribute(key);
            }
            catch (Exception ex)
            {
                SimpleLogHelper.Error(ex);
            }

            return(Rtn);
        }
Beispiel #14
0
        public static void Error(Exception ex, string title)
        {
            string msg = SimpleLogHelper.BeautyErrorMsg(ex, title);

            Error(msg);
        }