| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- 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
- {
- /// <summary>
- /// 通用操作
- /// </summary>
- public class CommonOperation
- {
- /// <summary>
- /// 判断是否long
- /// </summary>
- /// <param name="Number"></param>
- /// <returns></returns>
- public static bool IsLongNumber(string Number)
- {
- try
- {
- long.Parse(Number);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 判断是否int
- /// </summary>
- /// <param name="Number"></param>
- /// <returns></returns>
- public static bool IsIntNumber(string Number)
- {
- try
- {
- int.Parse(Number);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 判断是否电话号码
- /// </summary>
- /// <param name="Number"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 判断符合IP格式
- /// </summary>
- /// <param name="IPAddress"></param>
- /// <returns></returns>
- public static bool IsIPAddress(string IPAddress)
- {
- try
- {
- System.Net.IPAddress.Parse(IPAddress);
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 获取md5
- /// </summary>
- /// <param name="strSource"></param>
- /// <returns></returns>
- public static string StringToMd5(string strSource)
- {
- byte[] bytes = Encoding.UTF8.GetBytes(strSource);
- MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
- return BitConverter.ToString(provider.ComputeHash(bytes));
- }
- /// <summary>
- /// DES加密
- /// </summary>
- /// <param name="strSource"></param>
- /// <param name="Key"></param>
- /// <returns></returns>
- 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);
- //}
- /// <summary>
- /// DES解密
- /// </summary>
- /// <param name="strSource"></param>
- /// <param name="Key"></param>
- /// <returns></returns>
- public static string DESEncrypt(string strSource, string Key)
- {
- return pDESEncrypt(strSource, Key);
- }
- /// <summary>
- /// DES解密
- /// </summary>
- /// <param name="strSource"></param>
- /// <param name="Key"></param>
- /// <returns></returns>
- 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);
- //}
- /// <summary>
- /// DES解密
- /// </summary>
- /// <param name="strSource"></param>
- /// <param name="Key"></param>
- /// <returns></returns>
- public static string DESDecrypt(string strSource, string Key)
- {
- return pDESDecrypt(strSource, Key);
- }
- /// <summary>
- /// 设置Datagrid
- /// </summary>
- /// <param name="dataGridView"></param>
- /// <param name="AlternatingColor"></param>
- 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;
- }
- /// <summary>
- /// 设置Datagrid
- /// </summary>
- /// <param name="dataGridView"></param>
- 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;
- }
- /// <summary>
- /// 设置Datagrid
- /// </summary>
- /// <param name="dataGridView"></param>
- public static void SetDataGridStyle(DataGridView dataGridView)
- {
- SetDataGridStyleBlue(dataGridView);
- }
- /// <summary>
- /// 设置Datagrid
- /// </summary>
- /// <param name="dataGridView"></param>
- public static void SetDataGridStyleBlue(DataGridView dataGridView)
- {
- SetDataGridStyle(dataGridView, Color.FromArgb(229, 236, 246));
- }
- /// <summary>
- /// 设置Datagrid
- /// </summary>
- /// <param name="dataGridView"></param>
- public static void SetDataGridStyleRed(DataGridView dataGridView)
- {
- SetDataGridStyle(dataGridView, Color.FromArgb(246, 239, 229));
- }
- /// <summary>
- /// 根据生日获取年龄
- /// </summary>
- /// <param name="strBirthday"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 获取cpu id
- /// </summary>
- /// <returns></returns>
- 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 "";
- }
- }
- /// <summary>
- /// 获取磁盘编号
- /// </summary>
- /// <returns></returns>
- public static string GetDiskVolumeSerialNumber()
- {
- ManagementObject disk;
- disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
- disk.Get();
- return disk.GetPropertyValue("VolumeSerialNumber").ToString();
- }
- /// <summary>
- /// 获取本地ip
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取本地ip
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取mac地址
- /// </summary>
- /// <returns></returns>
- public static string GetMacByNetworkAddress()
- {
- string result = string.Empty;
- List<string> adds = GetMacByNetworkInterface();
- List<string> aL = adds.Where(a => a.Length == 12).ToList();
- if (aL.Count > 0)
- {
- result = aL[0];
- }
- return result;
- }
- /// <summary>
- /// 获取mac地址
- /// </summary>
- /// <returns></returns>
- private static List<string> GetMacByNetworkInterface()
- {
- List<string> macs = new List<string>();
- 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;
- }
- }
- /// <summary>
- /// 获取时间戳
- /// </summary>
- /// <returns></returns>
- public static long GetTimeStamp()
- {
- long TimeStamp = Convert.ToInt64((DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds);
- return TimeStamp;
- }
- /// <summary>
- /// 获取时间戳
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static long GetTimeStamp(DateTime time)
- {
- long TimeStamp = Convert.ToInt64((time - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds);
- return TimeStamp;
- }
- /// <summary>
- /// 根据时间戳获取时间
- /// </summary>
- /// <param name="timestamp"></param>
- /// <returns></returns>
- public static DateTime GetDateTimeFromTimeStamp(long timestamp)
- {
- DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1")).AddMilliseconds(timestamp);
- return dt;
- }
- /// <summary>
- /// 下载图片
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 下载图片
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
- /// <summary>
- /// 获取文件md5
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|