using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Security.Cryptography; using System.IO; using System.Drawing; using System.Windows.Forms; using System.Management; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Net.NetworkInformation; namespace HXX.Scanner.Common { /// /// 通用操作 /// public class CommonOperation { /// /// 判断是否long /// /// /// public static bool IsLongNumber(string Number) { try { long.Parse(Number); return true; } catch { return false; } } /// /// 判断是否int /// /// /// public static bool IsIntNumber(string Number) { try { int.Parse(Number); return true; } catch { return false; } } /// /// 判断是否电话号码 /// /// /// public static bool IsMobilePhoneNumber(string Number) { try { Number = long.Parse(Number.Trim()).ToString(); } catch { return false; } if (Number.Length == 11) { return true; } //if (Number.Length == 8) //{ // return true; //} return false; } public static string SBCToDBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) { c[i] = (char)(c[i] - 65248); } } return new string(c); } public static bool HasSBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { return true; } if (c[i] > 65280 && c[i] < 65375) return true; } return false; } /// /// 判断符合IP格式 /// /// /// public static bool IsIPAddress(string IPAddress) { try { System.Net.IPAddress.Parse(IPAddress); return true; } catch { return false; } } /// /// 获取md5 /// /// /// public static string StringToMd5(string strSource) { byte[] bytes = Encoding.UTF8.GetBytes(strSource); MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider(); return BitConverter.ToString(provider.ComputeHash(bytes)); } /// /// DES加密 /// /// /// /// private static string pDESEncrypt(string strSource, string Key) { string sKey = Key; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(strSource); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } //public static string DESEncrypt(string strSource) //{ // return pDESEncrypt(strSource, AppConst.DESKey); //} /// /// DES解密 /// /// /// /// public static string DESEncrypt(string strSource, string Key) { return pDESEncrypt(strSource, Key); } /// /// DES解密 /// /// /// /// private static string pDESDecrypt(string strSource, string Key) { string sKey = Key; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[strSource.Length / 2]; for (int x = 0; x < strSource.Length / 2; x++) { int i = (Convert.ToInt32(strSource.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } //public static string DESDecrypt(string strSource) //{ // return pDESDecrypt(strSource, AppConst.DESKey); //} /// /// DES解密 /// /// /// /// public static string DESDecrypt(string strSource, string Key) { return pDESDecrypt(strSource, Key); } /// /// 设置Datagrid /// /// /// private static void SetDataGridStyle(DataGridView dataGridView, Color AlternatingColor) { dataGridView.EnableHeadersVisualStyles = false; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(243, 244, 248); dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView.BackgroundColor = Color.White; dataGridView.DefaultCellStyle.BackColor = Color.White; dataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(229, 236, 246); dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None; dataGridView.AutoGenerateColumns = false; } /// /// 设置Datagrid /// /// public static void SetDataGridStyle_Input(DataGridView dataGridView) { dataGridView.EnableHeadersVisualStyles = false; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(243, 244, 248); dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView.BackgroundColor = Color.White; dataGridView.DefaultCellStyle.BackColor = Color.White; dataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(229, 236, 246); dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single; dataGridView.AutoGenerateColumns = false; } /// /// 设置Datagrid /// /// public static void SetDataGridStyle(DataGridView dataGridView) { SetDataGridStyleBlue(dataGridView); } /// /// 设置Datagrid /// /// public static void SetDataGridStyleBlue(DataGridView dataGridView) { SetDataGridStyle(dataGridView, Color.FromArgb(229, 236, 246)); } /// /// 设置Datagrid /// /// public static void SetDataGridStyleRed(DataGridView dataGridView) { SetDataGridStyle(dataGridView, Color.FromArgb(246, 239, 229)); } /// /// 根据生日获取年龄 /// /// /// public static string GetAge(string strBirthday) { try { DateTime dtBirthday = Convert.ToDateTime(strBirthday); TimeSpan span = DateTime.Now - dtBirthday; int age = Convert.ToInt32((span.Days / 365)) + 1; return age.ToString(); } catch { return string.Empty; } } /// /// 获取cpu id /// /// public static string GetCpuID() { try { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); string strCpuID = null; foreach (ManagementObject mo in moc) { strCpuID = mo.Properties["ProcessorId"].Value.ToString(); break; } return strCpuID; } catch { return ""; } } /// /// 获取磁盘编号 /// /// public static string GetDiskVolumeSerialNumber() { ManagementObject disk; disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } /// /// 获取本地ip /// /// public static string GetLocalIPV4() { IPAddress[] IAs = Dns.GetHostEntry(Dns.GetHostName()).AddressList; string IP = IAs[0].ToString(); foreach (IPAddress ip in IAs) { if (ip.AddressFamily == AddressFamily.InterNetwork) { IP = ip.ToString(); break; } } return IP; } /// /// 获取本地ip /// /// public static string GetLocalIPV4_v2() { IPAddress[] IAs = Dns.GetHostEntry(Dns.GetHostName()).AddressList; string IP = string.Empty; foreach (IPAddress ip in IAs) { if (ip.AddressFamily == AddressFamily.InterNetwork) { if (ip.ToString().StartsWith("192.168")) { IP = ip.ToString(); break; } } } if (IP.Length == 0) { IP = IAs[0].ToString(); foreach (IPAddress ip in IAs) { if (ip.AddressFamily == AddressFamily.InterNetwork) { IP = ip.ToString(); break; } } } return IP; } /// /// 获取mac地址 /// /// public static string GetMacByNetworkAddress() { string result = string.Empty; List adds = GetMacByNetworkInterface(); List aL = adds.Where(a => a.Length == 12).ToList(); if (aL.Count > 0) { result = aL[0]; } return result; } /// /// 获取mac地址 /// /// private static List GetMacByNetworkInterface() { List macs = new List(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { PhysicalAddress add = ni.GetPhysicalAddress(); if (add.ToString().Trim().Length > 0) { macs.Add(add.ToString()); } } return macs; } //public static void SetGroupBoxStyle(GroupBox GBX) //{ // GBX.Paint += new PaintEventHandler(GroupBox_Paint); //} //private static void GroupBox_Paint(object sender, PaintEventArgs e) //{ // Font font = new Font(GBX.Font, FontStyle.Bold); // Pen pen = new Pen(Color.FromArgb(74, 160, 213)); // SolidBrush brush = new SolidBrush(pen.Color); // e.Graphics.Clear(GBX.BackColor); // e.Graphics.DrawString(GBX.Text, font, brush, 13, 1); // e.Graphics.DrawLine(pen, 1, 7, 8, 7); // e.Graphics.DrawLine(pen, e.Graphics.MeasureString(GBX.Text, GBX.Font).Width + 18, 7, GBX.Width - 2, 7); // e.Graphics.DrawLine(pen, 1, 7, 1, GBX.Height - 2); // e.Graphics.DrawLine(pen, 1, GBX.Height - 2, GBX.Width - 2, GBX.Height - 2); // e.Graphics.DrawLine(pen, GBX.Width - 2, 7, GBX.Width - 2, GBX.Height - 2); //} public static bool CheckSymbol(string text) { Regex regex1 = new Regex(@"[,。;?~!:‘“”’【】()]"); Regex regex2 = new Regex(@"[~!@#\$%\^&\*\(\)\+=\|\\\}\]\{\[:;<,>\?\/""]+", RegexOptions.IgnorePatternWhitespace); if (regex1.IsMatch(text) || regex2.IsMatch(text)) { return true; } else { return false; } } /// /// 获取时间戳 /// /// public static long GetTimeStamp() { long TimeStamp = Convert.ToInt64((DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds); return TimeStamp; } /// /// 获取时间戳 /// /// /// public static long GetTimeStamp(DateTime time) { long TimeStamp = Convert.ToInt64((time - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds); return TimeStamp; } /// /// 根据时间戳获取时间 /// /// /// public static DateTime GetDateTimeFromTimeStamp(long timestamp) { DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1")).AddMilliseconds(timestamp); return dt; } /// /// 下载图片 /// /// /// public static Image DownloadPicture_Image(string url) { Image img = null; if (url.Length != 0) { try { WebClient wc = new WebClient(); byte[] bb = wc.DownloadData(url); using (MemoryStream ms = new MemoryStream(bb)) { img = Image.FromStream(ms); } } catch (Exception ee) { LogManager.WriteLog(ee); img = null; } } return img; } /// /// 下载图片 /// /// /// public static string DownloadPicture_Base64(string url) { string result = string.Empty; if (url.Length != 0) { try { WebClient wc = new WebClient(); byte[] bb = wc.DownloadData(url); result = Convert.ToBase64String(bb); } catch (Exception ee) { LogManager.WriteLog(ee); result = string.Empty; } } return result; } public static string GetWebExceptionMessage(Exception ee) { string result = string.Empty; System.Net.WebException webEE = ee as System.Net.WebException; if (webEE != null) { if (webEE.Response != null) { using (System.IO.Stream st = webEE.Response.GetResponseStream()) { using (System.IO.StreamReader sr = new System.IO.StreamReader(st)) { result = sr.ReadToEnd(); } } } } return result; } public static string SetBase64_string(string content) { string result = string.Empty; if (content.Length > 0) { byte[] bb = Encoding.UTF8.GetBytes(content); result = Convert.ToBase64String(bb); } return result; } public static string GetBase64_string(string content_base64) { string result = string.Empty; try { byte[] bb = Convert.FromBase64String(content_base64); result = Encoding.UTF8.GetString(bb); } catch { } return result; } public static bool IsFakeCard(string cardNumber) { if (cardNumber.StartsWith("f") || cardNumber.StartsWith("F")) { return true; } else { return false; } } public static string Translate_8H10_To_6H8D(string CardNumber) { string number = Convert.ToString(Convert.ToInt64(CardNumber), 16).ToUpper().PadLeft(8, '0'); number = number.Substring(2); number = Convert.ToInt64(number, 16).ToString().PadLeft(10, '0'); return number; } public static void DirectoryCopy(string source, string target) { if (!source.EndsWith("\\")) { source += "\\"; } if (!target.EndsWith("\\")) { target += "\\"; } string[] dirL = Directory.GetFileSystemEntries(source); foreach (string f in dirL) { if (Directory.Exists(f)) { string x = Path.GetDirectoryName(f); string folderName = f.Substring(f.LastIndexOf("\\") + 1); folderName = target + folderName; if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } DirectoryCopy(f, folderName); } else { string pf = Path.GetFileName(f); pf = target + pf; File.Copy(f, pf, true); } } } /// /// 获取文件md5 /// /// /// public static string GetFileMD5(string file) { string result = string.Empty; if (File.Exists(file)) { MD5CryptoServiceProvider m = new MD5CryptoServiceProvider(); using (FileStream fs = File.OpenRead(file)) { result = BitConverter.ToString(m.ComputeHash(fs)); } } return result; } } }