using System; using System.Collections.Specialized; using System.Data; using System.IO; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Xml.Serialization; namespace Common { public abstract class DataHelper { private static object Md5Mutex = new object(); public static void SerializeDataTableXmlFile(DataTable table, string filename) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataTable)); FileInfo fileInfo = new FileInfo(filename); if (!fileInfo.Directory.Exists) fileInfo.Directory.Create(); using (StreamWriter streamWriter = new StreamWriter((Stream)fileInfo.Open(FileMode.OpenOrCreate))) { try { xmlSerializer.Serialize((TextWriter)streamWriter, (object)table); } catch (Exception ex) { LogHelper.Error(ex); } } } public static int MemoryCompare(byte[] b1, byte[] b2) { int num = 0; if (b1.Length != b2.Length) { num = b1.Length - b2.Length; } else { for (int index = 0; index < b1.Length; ++index) { if ((int)b1[index] != (int)b2[index]) { num = (int)b1[index] - (int)b2[index]; break; } } } return num; } public static string MD5Hex(string source) { lock (DataHelper.Md5Mutex) return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(source))).Replace("-", ""); } public static string MD5Hex(byte[] source, int offset, int count) { lock (DataHelper.Md5Mutex) return BitConverter.ToString(MD5.Create().ComputeHash(source, offset, count)).Replace("-", ""); } public static byte[] MD5Bytes(string source) { lock (DataHelper.Md5Mutex) return MD5.Create().ComputeHash(Encoding.Default.GetBytes(source)); } private static string FormatToPlainText(string text) { char[] chArray = text.ToCharArray(); StringBuilder stringBuilder = new StringBuilder(); foreach (char ch in chArray) { if ((int)ch >= 48 && (int)ch <= 57 || (int)ch >= 65 && (int)ch <= 90 || (int)ch >= 97 && (int)ch <= 122 || (int)ch >= 16513 && (int)ch <= 65278) stringBuilder.Append(ch); } return stringBuilder.ToString(); } public static byte[] ComputerHashBytes(string source) { lock (DataHelper.Md5Mutex) return MD5.Create().ComputeHash(Encoding.Default.GetBytes(source)); } public static NameValueCollection GetParams(string exp) { NameValueCollection nameValueCollection = new NameValueCollection(); if (!string.IsNullOrEmpty(exp)) { foreach (string input in exp.Split(";\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { Match match = Regex.Match(input, "(.*)=(.*)"); if (match.Success) nameValueCollection[match.Groups[1].Value] = match.Groups[2].Value; } } return nameValueCollection; } public static string LeftString(string s, int length) { if (string.IsNullOrEmpty(s)) return string.Empty; return s.Substring(0, Math.Min(length, s.Length)); } public static string RemovePrefixString(string source, string prefix) { try { if (source.StartsWith(prefix)) return source.Substring(prefix.Length); return source; } catch { return source; } } public static string AddPrefixString(string source, string prefix) { if (!source.StartsWith(prefix)) return string.Format("{0}{1}", (object)prefix, (object)source); return source; } } }