/// <summary> /// 获取XElement文件树节点段XElement /// </summary> /// <param name="xml">XElement文件载体</param> /// <param name="mainnode">要查找的主节点</param> /// <param name="attribute1">主节点条件属性名1</param> /// <param name="value1">主节点条件属性值1</param> /// <param name="attribute2">主节点条件属性名2</param> /// <param name="value2">主节点条件属性值2</param> /// <returns>以该主节点为根的XElement</returns> public static XElement GetXElement(XElement XElement, string newroot, string attribute1, string value1, string attribute2, string value2) { if (XElement != null) { IEnumerable <XElement> xmlItems = XElement.DescendantsAndSelf(newroot); foreach (var xmlItem in xmlItems) { XAttribute attrib1 = xmlItem.Attribute(attribute1); XAttribute attrib2 = xmlItem.Attribute(attribute2); if (null != attrib1 && attrib1.Value == value1) { if (null != attrib2 && attrib2.Value == value2) { return(xmlItem); } } } return(null); } else { DebugMod.LogError(new Exception(string.Format("GetXElement异常, 读取: {0}/{1}={2}/{3}={4} 失败, xml节点名: {5}", newroot, attribute1, value1, attribute2, value2, GetXElementNodePath(XElement)))); return(null); } }
/// <summary> /// 安全获取xml整型数值 /// </summary> /// <param name="XElement"></param> /// <param name="attributeName"></param> /// <returns></returns> public static float GetXElementAttributeFloat(XElement XElement, string attributeName) { XAttribute attrib = GetAttribute(XElement, attributeName); if (null == attrib) { return(-1.0f); } string str = (string)attrib; if (null == str || str == "") { return(-1.0f); } float nReturn = 0.0f; if (float.TryParse(str, out nReturn)) { return(nReturn); } else { DebugMod.LogError(string.Format("读取属性: {0} 失败, xml节点名: {1}", attributeName, GetXElementNodePath(XElement))); return(-1.0f); } }
public static List <int> GetXElementAttributeIntList(XElement xElement, string attributeName, string newroot = null) { List <int> result = new List <int>(); if (xElement != null) { IEnumerable <XElement> xmlItems = null; if (null == newroot) { xmlItems = xElement.Elements(); } else { xmlItems = xElement.DescendantsAndSelf(newroot); } if (xmlItems != null) { foreach (var xmlItem in xmlItems) { result.Add(GetXElementAttributeInt(xmlItem, attributeName)); } } } else { DebugMod.LogError(new Exception(string.Format("GetXElement异常, 读取: {0} 失败, xml节点名: {1}", newroot, GetXElementNodePath(xElement)))); } return(result); }
/// <summary> /// 获取XElement文件树节点段XElement /// </summary> /// <param name="xml">XElement文件载体</param> /// <param name="mainnode">要查找的主节点</param> /// <param name="attribute">主节点条件属性名</param> /// <param name="value">主节点条件属性值</param> /// <returns>以该主节点为根的XElement</returns> public static XElement GetXElement(XElement XElement, string newroot, string attribute, string value) { if (XElement != null) { IEnumerable <XElement> xmlItems = XElement.DescendantsAndSelf(newroot); if (xmlItems != null) { foreach (var xmlItem in xmlItems) { XAttribute attrib = null; if (xmlItem != null) { attrib = xmlItem.Attribute(attribute); } if (null != attrib && attrib.Value == value) { return(xmlItem); } } } return(null); } else { DebugMod.LogError(new Exception(string.Format("GetXElement异常, 读取: {0}/{1}={2} 失败, xml节点名: {3}", newroot, attribute, value, GetXElementNodePath(XElement)))); return(null); } }
/// <summary> /// 输入明文和密钥,输出密文 /// </summary> /// <param name="plainText"></param> /// <param name="key"></param> /// <returns></returns> public static string Encrypt(string plainText, string passwd, string saltValue) { if (string.IsNullOrEmpty(plainText)) { return(null); } byte[] bytesData = null; try { bytesData = new UTF8Encoding().GetBytes(plainText); } catch (Exception e) { DebugMod.LogException(e); return(null); } byte[] bytesResult = null; try { bytesResult = AesHelper.AesEncryptBytes(bytesData, passwd, saltValue); } catch (Exception e) { DebugMod.LogException(e); return(null); } return(ByteHelper.Bytes2HexString(bytesResult)); }
public static void LogException(Exception e, string extMsg, bool showMsgBox) { try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("应用程序出现了异常:\r\n{0}\r\n", e.Message); stringBuilder.AppendFormat("\r\n 额外信息: {0}", extMsg); if (null != e) { if (e.InnerException != null) { stringBuilder.AppendFormat("\r\n {0}", e.InnerException.Message); } stringBuilder.AppendFormat("\r\n {0}", e.StackTrace); } //记录异常日志文件 //LogManager.WriteException(stringBuilder.ToString()); if (showMsgBox) { //弹出异常日志窗口 System.Console.WriteLine(stringBuilder.ToString()); } } catch (Exception ex) { DebugMod.LogException(ex); } }
/// <summary> /// 将日期时间字符串转为整数表示 /// </summary> /// <param name="str"></param> /// <returns></returns> public static long ConvertToTicks(string str, long defVal) { if ("*" == str) { return(defVal); } str = str.Replace('$', ':'); try { DateTime dt; if (!DateTime.TryParse(str, out dt)) { return(0L); } return(dt.Ticks / 10000); } catch (Exception e) { DebugMod.LogException(e); } return(0L); }
/// <summary> /// 获取XElement文件树节点段XElement的列表 /// </summary> /// <param name="XElement">XElement文件载体</param> /// <param name="newroot">要查找的独立节点</param> /// <returns>XElement列表</returns> public static List <XElement> GetXElementList(XElement xElement, string newroot = null) { List <XElement> xmlItemList = new List <XElement>(); if (xElement != null) { IEnumerable <XElement> xmlItems = null; if (null == newroot) { xmlItems = xElement.Elements(); } else { xmlItems = xElement.DescendantsAndSelf(newroot); } if (xmlItems != null) { foreach (var xmlItem in xmlItems) { xmlItemList.Insert(xmlItemList.Count, xmlItem); } } } else { DebugMod.LogError(new Exception(string.Format("GetXElement异常, 读取: {0} 失败, xml节点名: {1}", newroot, GetXElementNodePath(xElement)))); } return(xmlItemList); }
/// <summary> /// 获得csvmod加载的文件内容,并按照“前后名id”,放到NameContainer的各自的List中(0->只做前名,1->只做后名,2->前后名都可以做) /// NameContainer< <0, <前名...> > , <1, <后名...> , <2 ,<前/后...> > > /// </summary> private void InitNameDictionary() { if (0 == (tempList = csvmod.GetAllData()).Count)//若是没加载Name.csv文件,先加载. { if (!csvmod.LoadCsvStr()) { DebugMod.Log("Load NameFile Failed!"); } else { for (int index = 0; index < tempList.Count; index++) { int nameId = int.Parse(tempList[index]["前后代号"]); //前后代号:各种名字的id string name = tempList[index]["备用名字"]; // 代号对应的名字 if (NameContainer.ContainsKey(nameId)) { NameContainer[nameId].Add(name); } else { NameContainer.Add(nameId, new List <string>()); } } } } }
/// <summary> /// 加载读取csv字符串 /// </summary> public bool LoadCsvStr() { if (String.IsNullOrEmpty(this.csvStr)) { //csv文件路径不存在 DebugMod.LogError(new Exception(string.Format("csv信息为空"))); return(false); } string[] csvInfoArray = csvStr.Split(new string[] { "\r\n" }, StringSplitOptions.None); string csvInfoNameDataLine = csvInfoArray[0]; ParseCsvInfoName(csvInfoNameDataLine); //从csv文件中解析出信息的名称 string csvDataLine = ""; //表示一行csv文件数据 for (int i = 1; i < csvInfoArray.Length; ++i) { string fileDataLine; //表示读取出的一行数据 fileDataLine = csvInfoArray[i]; if (String.IsNullOrEmpty(fileDataLine)) //表示读取到了文件结尾,跳出循环 { break; } if ("" == csvDataLine) { csvDataLine = fileDataLine; } else { csvDataLine += "\r\n" + fileDataLine; } if (!IfOddQuotation(csvDataLine)) //此时csvDataLine中包含偶数个引号,表示是完整的一行csv数据,如果csvDataLine中包含奇数个引号,表示不是完整的一行csv数据,数据被回车换行符分割。 { AddNewDataLine(csvDataLine); csvDataLine = ""; } } if (csvDataLine.Length > 0) //在读取玩全部csv数据后,如果csvDataLine中仍然存在数据,表示数据中出现了奇数个引号,csv文件格式错误。 { //csv文件格式错误 DebugMod.LogError(new Exception(string.Format("csv文件格式错误"))); return(false); } return(true); }
public int PutData(byte[] byteData, int offset, int len) { if (len <= 0 || len > _maxLen) { //PRINT("CCircularBuffer::PutData len is <=0\n"); return(0); } while (IsOverFlowCondition(len)) { BufferResize(); } m_nValidCount += len; try { if (IsIndexOverFlow(len)) { int FirstCopyLen = m_iBufSize - m_iTailPos; int SecondCopyLen = len - FirstCopyLen; Tools.Assert.Check(FirstCopyLen > 0); Array.Copy(byteData, offset, m_Buffer, m_iTailPos, FirstCopyLen); if (SecondCopyLen > 0) { Array.Copy(byteData, offset + FirstCopyLen, m_Buffer, 0, SecondCopyLen); m_iTailPos = SecondCopyLen; } else { m_iTailPos = 0; } } else { Array.Copy(byteData, offset, m_Buffer, m_iTailPos, len); m_iTailPos += len; } } catch (Exception e) { DebugMod.LogException(e, "CircleBuffer.PutData", false); } return(len); }
/// <summary> /// 判断如果不是 "*", 则转为指定的值, 否则默认值 /// </summary> /// <param name="str"></param> /// <returns></returns> public static Int32 ConvertToInt32(string str, Int32 defVal) { try { if ("*" != str) { return(Convert.ToInt32(str)); } return(defVal); } catch (Exception e) { DebugMod.LogException(e); } return(defVal); }
/// <summary> /// 格式化堆栈信息 /// </summary> /// <param name="msg"></param> /// <returns></returns> public static void LogFormatStack(System.Diagnostics.StackTrace stackTrace, string extMsg) { try { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("应用程序出现了对象锁定超时错误:\r\n"); stringBuilder.AppendFormat("\r\n 额外信息: {0}", extMsg); stringBuilder.AppendFormat("\r\n {0}", stackTrace.ToString()); //记录异常日志文件 //LogManager.WriteException(stringBuilder.ToString()); } catch (Exception e) { DebugMod.LogException(e); } }
/// <summary> /// 将日期时间字符串转为整数表示 /// </summary> /// <param name="str"></param> /// <returns></returns> public static long ConvertToTicks(string str) { try { DateTime dt; if (!DateTime.TryParse(str, out dt)) { return(0L); } return(dt.Ticks / 10000); } catch (Exception e) { DebugMod.LogException(e); } return(0L); }
/// <summary> /// 去掉数据块的首尾引号,一对双引号变为单个单引号 /// </summary> /// <param name="fileCellData"></param> /// <returns></returns> private string GetHandleData(string fileCellData) { if ("" == fileCellData) { return(""); } if (IfOddQuotationStart(fileCellData)) { if (IfOddQuotationEnd(fileCellData)) { return(fileCellData.Substring(1, fileCellData.Length - 2).Replace("\"\"", "\"")); //去掉数据块的首尾引号,一对双引号变为单个单引号 } else { DebugMod.LogError(new Exception(string.Format("csv数据引号无法匹配, 无法匹配数据:{0}", fileCellData))); } } return(fileCellData); }
/// <summary> /// 输入密文和密钥,输出明文 /// </summary> /// <param name="plainText"></param> /// <param name="key"></param> /// <returns></returns> public static string Decrypt(string encryptText, string passwd, string saltValue) { if (string.IsNullOrEmpty(encryptText)) { return(null); } byte[] bytesData = ByteHelper.HexString2Bytes(encryptText); if (null == bytesData) { return(null); } byte[] bytesResult = null; try { bytesResult = AesHelper.AesDecryptBytes(bytesData, passwd, saltValue); } catch (Exception e) { DebugMod.LogException(e); return(null); } string strResult = null; try { strResult = new UTF8Encoding().GetString(bytesResult, 0, bytesResult.Length); } //解析错误 catch (Exception e) { DebugMod.LogException(e); return(null); } return(strResult); }
/// <summary> /// AES解密字节流 /// </summary> /// <param name="encryptData">密文字节流</param> /// <param name="passwd">密码</param> /// <param name="saltValue">盐值</param> /// <returns>解密后的明文字节流</returns> public static byte[] AesDecryptBytes(byte[] encryptData, string passwd, string saltValue) { // 盐值(与加密时设置的值必须一致) // 密码值(与加密时设置的值必须一致) byte[] saltBytes = Encoding.UTF8.GetBytes(saltValue); AesManaged aes = new AesManaged(); Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(passwd, saltBytes); aes.BlockSize = aes.LegalBlockSizes[0].MaxSize; aes.KeySize = aes.LegalKeySizes[0].MaxSize; aes.Key = rfc.GetBytes(aes.KeySize / 8); aes.IV = rfc.GetBytes(aes.BlockSize / 8); // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象 ICryptoTransform decryptTransform = aes.CreateDecryptor(); // 解密后的输出流 MemoryStream decryptStream = new MemoryStream(); // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接 CryptoStream decryptor = new CryptoStream(decryptStream, decryptTransform, CryptoStreamMode.Write); try { // 将一个字节序列写入当前 CryptoStream (完成解密的过程) decryptor.Write(encryptData, 0, encryptData.Length); decryptor.Close(); } catch (Exception e) { DebugMod.LogException(e); return(null); //解密失败 } // 将解密后所得到的流转换为字符串 return(decryptStream.ToArray()); }
/// <summary> /// 获取指定的xml节点的节点路径 /// </summary> /// <param name="element"></param> static string GetXElementNodePath(XElement element) { if (element == null) { return(null); } try { string path = element.Name.ToString(); element = element.Parent; while (null != element) { path = element.Name.ToString() + "/" + path; element = element.Parent; } return(path); } catch (Exception e) { DebugMod.LogError(e.Message); } return(null); }
/// <summary> /// 获取属性值 /// </summary> /// <param name="XElement"></param> /// <param name="attribute"></param> /// <returns></returns> public static XAttribute GetAttribute(XElement XElement, string attribute) { if (null == XElement) { return(null); } try { XAttribute attrib = XElement.Attribute(attribute); if (null == attrib) { DebugMod.LogError(new Exception(string.Format("读取属性: {0} 失败, xml节点名: {1}", attribute, GetXElementNodePath(XElement)))); return(null); } return(attrib); } catch { DebugMod.LogError(new Exception(string.Format("读取属性: {0} 失败, xml节点名: {1}", attribute, GetXElementNodePath(XElement)))); return(null); } }
/// <summary> /// 加载读取csv文件 /// </summary> public bool LoadCsvFile() { if (String.IsNullOrEmpty(this.csvFilePath)) { //csv文件路径不存在 DebugMod.LogError(new Exception(string.Format("csv文件路径不存在, csv文件路径:{0}", this.csvFilePath))); return(false); } else if (!File.Exists(this.csvFilePath)) { //csv文件不存在 DebugMod.LogError(new Exception(string.Format("csv文件不存在, csv文件路径:{0}", this.csvFilePath))); return(false); } if (null == this.encoding) { this.encoding = Encoding.Default; } StreamReader sr = new StreamReader(this.csvFilePath, this.encoding); string csvInfoNameDataLine = sr.ReadLine(); if (String.IsNullOrEmpty(csvInfoNameDataLine)) //表示此文件为空 { DebugMod.LogError(new Exception(string.Format("此csv文件为空, csv文件路径:{0}", this.csvFilePath))); sr.Close(); return(false); } ParseCsvInfoName(csvInfoNameDataLine); //从csv文件中解析出信息的名称 string csvDataLine = ""; //表示一行csv文件数据 while (true) { string fileDataLine; //表示读取出的一行数据 fileDataLine = sr.ReadLine(); if (String.IsNullOrEmpty(fileDataLine)) //表示读取到了文件结尾,跳出循环 { break; } if ("" == csvDataLine) { csvDataLine = fileDataLine; } else { csvDataLine += "\r\n" + fileDataLine; } if (!IfOddQuotation(csvDataLine)) //此时csvDataLine中包含偶数个引号,表示是完整的一行csv数据,如果csvDataLine中包含奇数个引号,表示不是完整的一行csv数据,数据被回车换行符分割。 { AddNewDataLine(csvDataLine); csvDataLine = ""; } } sr.Close(); if (csvDataLine.Length > 0) //在读取玩全部csv数据后,如果csvDataLine中仍然存在数据,表示数据中出现了奇数个引号,csv文件格式错误。 { //csv文件格式错误 DebugMod.LogError(new Exception(string.Format("csv文件格式错误, csv文件路径:{0}", this.csvFilePath))); return(false); } return(true); }