CommonOperation.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Security.Cryptography;
  7. using System.IO;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using System.Management;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Reflection;
  14. using System.Net.NetworkInformation;
  15. namespace HXX.Scanner.Common
  16. {
  17. /// <summary>
  18. /// 通用操作
  19. /// </summary>
  20. public class CommonOperation
  21. {
  22. /// <summary>
  23. /// 判断是否long
  24. /// </summary>
  25. /// <param name="Number"></param>
  26. /// <returns></returns>
  27. public static bool IsLongNumber(string Number)
  28. {
  29. try
  30. {
  31. long.Parse(Number);
  32. return true;
  33. }
  34. catch
  35. {
  36. return false;
  37. }
  38. }
  39. /// <summary>
  40. /// 判断是否int
  41. /// </summary>
  42. /// <param name="Number"></param>
  43. /// <returns></returns>
  44. public static bool IsIntNumber(string Number)
  45. {
  46. try
  47. {
  48. int.Parse(Number);
  49. return true;
  50. }
  51. catch
  52. {
  53. return false;
  54. }
  55. }
  56. /// <summary>
  57. /// 判断是否电话号码
  58. /// </summary>
  59. /// <param name="Number"></param>
  60. /// <returns></returns>
  61. public static bool IsMobilePhoneNumber(string Number)
  62. {
  63. try
  64. {
  65. Number = long.Parse(Number.Trim()).ToString();
  66. }
  67. catch { return false; }
  68. if (Number.Length == 11)
  69. {
  70. return true;
  71. }
  72. //if (Number.Length == 8)
  73. //{
  74. // return true;
  75. //}
  76. return false;
  77. }
  78. public static string SBCToDBC(string input)
  79. {
  80. char[] c = input.ToCharArray();
  81. for (int i = 0; i < c.Length; i++)
  82. {
  83. if (c[i] == 12288)
  84. {
  85. c[i] = (char)32;
  86. continue;
  87. }
  88. if (c[i] > 65280 && c[i] < 65375)
  89. {
  90. c[i] = (char)(c[i] - 65248);
  91. }
  92. }
  93. return new string(c);
  94. }
  95. public static bool HasSBC(string input)
  96. {
  97. char[] c = input.ToCharArray();
  98. for (int i = 0; i < c.Length; i++)
  99. {
  100. if (c[i] == 12288)
  101. {
  102. return true;
  103. }
  104. if (c[i] > 65280 && c[i] < 65375)
  105. return true;
  106. }
  107. return false;
  108. }
  109. /// <summary>
  110. /// 判断符合IP格式
  111. /// </summary>
  112. /// <param name="IPAddress"></param>
  113. /// <returns></returns>
  114. public static bool IsIPAddress(string IPAddress)
  115. {
  116. try
  117. {
  118. System.Net.IPAddress.Parse(IPAddress);
  119. return true;
  120. }
  121. catch
  122. {
  123. return false;
  124. }
  125. }
  126. /// <summary>
  127. /// 获取md5
  128. /// </summary>
  129. /// <param name="strSource"></param>
  130. /// <returns></returns>
  131. public static string StringToMd5(string strSource)
  132. {
  133. byte[] bytes = Encoding.UTF8.GetBytes(strSource);
  134. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  135. return BitConverter.ToString(provider.ComputeHash(bytes));
  136. }
  137. /// <summary>
  138. /// DES加密
  139. /// </summary>
  140. /// <param name="strSource"></param>
  141. /// <param name="Key"></param>
  142. /// <returns></returns>
  143. private static string pDESEncrypt(string strSource, string Key)
  144. {
  145. string sKey = Key;
  146. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  147. byte[] inputByteArray = Encoding.Default.GetBytes(strSource);
  148. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  149. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  150. MemoryStream ms = new MemoryStream();
  151. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  152. cs.Write(inputByteArray, 0, inputByteArray.Length);
  153. cs.FlushFinalBlock();
  154. StringBuilder ret = new StringBuilder();
  155. foreach (byte b in ms.ToArray())
  156. {
  157. ret.AppendFormat("{0:X2}", b);
  158. }
  159. ret.ToString();
  160. return ret.ToString();
  161. }
  162. //public static string DESEncrypt(string strSource)
  163. //{
  164. // return pDESEncrypt(strSource, AppConst.DESKey);
  165. //}
  166. /// <summary>
  167. /// DES解密
  168. /// </summary>
  169. /// <param name="strSource"></param>
  170. /// <param name="Key"></param>
  171. /// <returns></returns>
  172. public static string DESEncrypt(string strSource, string Key)
  173. {
  174. return pDESEncrypt(strSource, Key);
  175. }
  176. /// <summary>
  177. /// DES解密
  178. /// </summary>
  179. /// <param name="strSource"></param>
  180. /// <param name="Key"></param>
  181. /// <returns></returns>
  182. private static string pDESDecrypt(string strSource, string Key)
  183. {
  184. string sKey = Key;
  185. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  186. byte[] inputByteArray = new byte[strSource.Length / 2];
  187. for (int x = 0; x < strSource.Length / 2; x++)
  188. {
  189. int i = (Convert.ToInt32(strSource.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i;
  190. }
  191. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  192. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  193. MemoryStream ms = new MemoryStream();
  194. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  195. cs.Write(inputByteArray, 0, inputByteArray.Length);
  196. cs.FlushFinalBlock();
  197. StringBuilder ret = new StringBuilder();
  198. return System.Text.Encoding.Default.GetString(ms.ToArray());
  199. }
  200. //public static string DESDecrypt(string strSource)
  201. //{
  202. // return pDESDecrypt(strSource, AppConst.DESKey);
  203. //}
  204. /// <summary>
  205. /// DES解密
  206. /// </summary>
  207. /// <param name="strSource"></param>
  208. /// <param name="Key"></param>
  209. /// <returns></returns>
  210. public static string DESDecrypt(string strSource, string Key)
  211. {
  212. return pDESDecrypt(strSource, Key);
  213. }
  214. /// <summary>
  215. /// 设置Datagrid
  216. /// </summary>
  217. /// <param name="dataGridView"></param>
  218. /// <param name="AlternatingColor"></param>
  219. private static void SetDataGridStyle(DataGridView dataGridView, Color AlternatingColor)
  220. {
  221. dataGridView.EnableHeadersVisualStyles = false;
  222. dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(243, 244, 248);
  223. dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  224. dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  225. dataGridView.BackgroundColor = Color.White;
  226. dataGridView.DefaultCellStyle.BackColor = Color.White;
  227. dataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(229, 236, 246);
  228. dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None;
  229. dataGridView.AutoGenerateColumns = false;
  230. }
  231. /// <summary>
  232. /// 设置Datagrid
  233. /// </summary>
  234. /// <param name="dataGridView"></param>
  235. public static void SetDataGridStyle_Input(DataGridView dataGridView)
  236. {
  237. dataGridView.EnableHeadersVisualStyles = false;
  238. dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(243, 244, 248);
  239. dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  240. dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  241. dataGridView.BackgroundColor = Color.White;
  242. dataGridView.DefaultCellStyle.BackColor = Color.White;
  243. dataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(229, 236, 246);
  244. dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
  245. dataGridView.AutoGenerateColumns = false;
  246. }
  247. /// <summary>
  248. /// 设置Datagrid
  249. /// </summary>
  250. /// <param name="dataGridView"></param>
  251. public static void SetDataGridStyle(DataGridView dataGridView)
  252. {
  253. SetDataGridStyleBlue(dataGridView);
  254. }
  255. /// <summary>
  256. /// 设置Datagrid
  257. /// </summary>
  258. /// <param name="dataGridView"></param>
  259. public static void SetDataGridStyleBlue(DataGridView dataGridView)
  260. {
  261. SetDataGridStyle(dataGridView, Color.FromArgb(229, 236, 246));
  262. }
  263. /// <summary>
  264. /// 设置Datagrid
  265. /// </summary>
  266. /// <param name="dataGridView"></param>
  267. public static void SetDataGridStyleRed(DataGridView dataGridView)
  268. {
  269. SetDataGridStyle(dataGridView, Color.FromArgb(246, 239, 229));
  270. }
  271. /// <summary>
  272. /// 根据生日获取年龄
  273. /// </summary>
  274. /// <param name="strBirthday"></param>
  275. /// <returns></returns>
  276. public static string GetAge(string strBirthday)
  277. {
  278. try
  279. {
  280. DateTime dtBirthday = Convert.ToDateTime(strBirthday);
  281. TimeSpan span = DateTime.Now - dtBirthday;
  282. int age = Convert.ToInt32((span.Days / 365)) + 1;
  283. return age.ToString();
  284. }
  285. catch
  286. {
  287. return string.Empty;
  288. }
  289. }
  290. /// <summary>
  291. /// 获取cpu id
  292. /// </summary>
  293. /// <returns></returns>
  294. public static string GetCpuID()
  295. {
  296. try
  297. {
  298. ManagementClass mc = new ManagementClass("Win32_Processor");
  299. ManagementObjectCollection moc = mc.GetInstances();
  300. string strCpuID = null;
  301. foreach (ManagementObject mo in moc)
  302. {
  303. strCpuID = mo.Properties["ProcessorId"].Value.ToString();
  304. break;
  305. }
  306. return strCpuID;
  307. }
  308. catch
  309. {
  310. return "";
  311. }
  312. }
  313. /// <summary>
  314. /// 获取磁盘编号
  315. /// </summary>
  316. /// <returns></returns>
  317. public static string GetDiskVolumeSerialNumber()
  318. {
  319. ManagementObject disk;
  320. disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  321. disk.Get();
  322. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  323. }
  324. /// <summary>
  325. /// 获取本地ip
  326. /// </summary>
  327. /// <returns></returns>
  328. public static string GetLocalIPV4()
  329. {
  330. IPAddress[] IAs = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
  331. string IP = IAs[0].ToString();
  332. foreach (IPAddress ip in IAs)
  333. {
  334. if (ip.AddressFamily == AddressFamily.InterNetwork)
  335. {
  336. IP = ip.ToString();
  337. break;
  338. }
  339. }
  340. return IP;
  341. }
  342. /// <summary>
  343. /// 获取本地ip
  344. /// </summary>
  345. /// <returns></returns>
  346. public static string GetLocalIPV4_v2()
  347. {
  348. IPAddress[] IAs = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
  349. string IP = string.Empty;
  350. foreach (IPAddress ip in IAs)
  351. {
  352. if (ip.AddressFamily == AddressFamily.InterNetwork)
  353. {
  354. if (ip.ToString().StartsWith("192.168"))
  355. {
  356. IP = ip.ToString();
  357. break;
  358. }
  359. }
  360. }
  361. if (IP.Length == 0)
  362. {
  363. IP = IAs[0].ToString();
  364. foreach (IPAddress ip in IAs)
  365. {
  366. if (ip.AddressFamily == AddressFamily.InterNetwork)
  367. {
  368. IP = ip.ToString();
  369. break;
  370. }
  371. }
  372. }
  373. return IP;
  374. }
  375. /// <summary>
  376. /// 获取mac地址
  377. /// </summary>
  378. /// <returns></returns>
  379. public static string GetMacByNetworkAddress()
  380. {
  381. string result = string.Empty;
  382. List<string> adds = GetMacByNetworkInterface();
  383. List<string> aL = adds.Where(a => a.Length == 12).ToList();
  384. if (aL.Count > 0)
  385. {
  386. result = aL[0];
  387. }
  388. return result;
  389. }
  390. /// <summary>
  391. /// 获取mac地址
  392. /// </summary>
  393. /// <returns></returns>
  394. private static List<string> GetMacByNetworkInterface()
  395. {
  396. List<string> macs = new List<string>();
  397. NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
  398. foreach (NetworkInterface ni in interfaces)
  399. {
  400. PhysicalAddress add = ni.GetPhysicalAddress();
  401. if (add.ToString().Trim().Length > 0)
  402. {
  403. macs.Add(add.ToString());
  404. }
  405. }
  406. return macs;
  407. }
  408. //public static void SetGroupBoxStyle(GroupBox GBX)
  409. //{
  410. // GBX.Paint += new PaintEventHandler(GroupBox_Paint);
  411. //}
  412. //private static void GroupBox_Paint(object sender, PaintEventArgs e)
  413. //{
  414. // Font font = new Font(GBX.Font, FontStyle.Bold);
  415. // Pen pen = new Pen(Color.FromArgb(74, 160, 213));
  416. // SolidBrush brush = new SolidBrush(pen.Color);
  417. // e.Graphics.Clear(GBX.BackColor);
  418. // e.Graphics.DrawString(GBX.Text, font, brush, 13, 1);
  419. // e.Graphics.DrawLine(pen, 1, 7, 8, 7);
  420. // e.Graphics.DrawLine(pen, e.Graphics.MeasureString(GBX.Text, GBX.Font).Width + 18, 7, GBX.Width - 2, 7);
  421. // e.Graphics.DrawLine(pen, 1, 7, 1, GBX.Height - 2);
  422. // e.Graphics.DrawLine(pen, 1, GBX.Height - 2, GBX.Width - 2, GBX.Height - 2);
  423. // e.Graphics.DrawLine(pen, GBX.Width - 2, 7, GBX.Width - 2, GBX.Height - 2);
  424. //}
  425. public static bool CheckSymbol(string text)
  426. {
  427. Regex regex1 = new Regex(@"[,。;?~!:‘“”’【】()]");
  428. Regex regex2 = new Regex(@"[~!@#\$%\^&\*\(\)\+=\|\\\}\]\{\[:;<,>\?\/""]+", RegexOptions.IgnorePatternWhitespace);
  429. if (regex1.IsMatch(text) || regex2.IsMatch(text))
  430. {
  431. return true;
  432. }
  433. else
  434. {
  435. return false;
  436. }
  437. }
  438. /// <summary>
  439. /// 获取时间戳
  440. /// </summary>
  441. /// <returns></returns>
  442. public static long GetTimeStamp()
  443. {
  444. long TimeStamp = Convert.ToInt64((DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds);
  445. return TimeStamp;
  446. }
  447. /// <summary>
  448. /// 获取时间戳
  449. /// </summary>
  450. /// <param name="time"></param>
  451. /// <returns></returns>
  452. public static long GetTimeStamp(DateTime time)
  453. {
  454. long TimeStamp = Convert.ToInt64((time - TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1"))).TotalMilliseconds);
  455. return TimeStamp;
  456. }
  457. /// <summary>
  458. /// 根据时间戳获取时间
  459. /// </summary>
  460. /// <param name="timestamp"></param>
  461. /// <returns></returns>
  462. public static DateTime GetDateTimeFromTimeStamp(long timestamp)
  463. {
  464. DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Parse("1970-1-1")).AddMilliseconds(timestamp);
  465. return dt;
  466. }
  467. /// <summary>
  468. /// 下载图片
  469. /// </summary>
  470. /// <param name="url"></param>
  471. /// <returns></returns>
  472. public static Image DownloadPicture_Image(string url)
  473. {
  474. Image img = null;
  475. if (url.Length != 0)
  476. {
  477. try
  478. {
  479. WebClient wc = new WebClient();
  480. byte[] bb = wc.DownloadData(url);
  481. using (MemoryStream ms = new MemoryStream(bb))
  482. {
  483. img = Image.FromStream(ms);
  484. }
  485. }
  486. catch (Exception ee)
  487. {
  488. LogManager.WriteLog(ee);
  489. img = null;
  490. }
  491. }
  492. return img;
  493. }
  494. /// <summary>
  495. /// 下载图片
  496. /// </summary>
  497. /// <param name="url"></param>
  498. /// <returns></returns>
  499. public static string DownloadPicture_Base64(string url)
  500. {
  501. string result = string.Empty;
  502. if (url.Length != 0)
  503. {
  504. try
  505. {
  506. WebClient wc = new WebClient();
  507. byte[] bb = wc.DownloadData(url);
  508. result = Convert.ToBase64String(bb);
  509. }
  510. catch (Exception ee)
  511. {
  512. LogManager.WriteLog(ee);
  513. result = string.Empty;
  514. }
  515. }
  516. return result;
  517. }
  518. public static string GetWebExceptionMessage(Exception ee)
  519. {
  520. string result = string.Empty;
  521. System.Net.WebException webEE = ee as System.Net.WebException;
  522. if (webEE != null)
  523. {
  524. if (webEE.Response != null)
  525. {
  526. using (System.IO.Stream st = webEE.Response.GetResponseStream())
  527. {
  528. using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
  529. {
  530. result = sr.ReadToEnd();
  531. }
  532. }
  533. }
  534. }
  535. return result;
  536. }
  537. public static string SetBase64_string(string content)
  538. {
  539. string result = string.Empty;
  540. if (content.Length > 0)
  541. {
  542. byte[] bb = Encoding.UTF8.GetBytes(content);
  543. result = Convert.ToBase64String(bb);
  544. }
  545. return result;
  546. }
  547. public static string GetBase64_string(string content_base64)
  548. {
  549. string result = string.Empty;
  550. try
  551. {
  552. byte[] bb = Convert.FromBase64String(content_base64);
  553. result = Encoding.UTF8.GetString(bb);
  554. }
  555. catch { }
  556. return result;
  557. }
  558. public static bool IsFakeCard(string cardNumber)
  559. {
  560. if (cardNumber.StartsWith("f") || cardNumber.StartsWith("F"))
  561. {
  562. return true;
  563. }
  564. else
  565. {
  566. return false;
  567. }
  568. }
  569. public static string Translate_8H10_To_6H8D(string CardNumber)
  570. {
  571. string number = Convert.ToString(Convert.ToInt64(CardNumber), 16).ToUpper().PadLeft(8, '0');
  572. number = number.Substring(2);
  573. number = Convert.ToInt64(number, 16).ToString().PadLeft(10, '0');
  574. return number;
  575. }
  576. public static void DirectoryCopy(string source, string target)
  577. {
  578. if (!source.EndsWith("\\"))
  579. {
  580. source += "\\";
  581. }
  582. if (!target.EndsWith("\\"))
  583. {
  584. target += "\\";
  585. }
  586. string[] dirL = Directory.GetFileSystemEntries(source);
  587. foreach (string f in dirL)
  588. {
  589. if (Directory.Exists(f))
  590. {
  591. string x = Path.GetDirectoryName(f);
  592. string folderName = f.Substring(f.LastIndexOf("\\") + 1);
  593. folderName = target + folderName;
  594. if (!Directory.Exists(folderName))
  595. {
  596. Directory.CreateDirectory(folderName);
  597. }
  598. DirectoryCopy(f, folderName);
  599. }
  600. else
  601. {
  602. string pf = Path.GetFileName(f);
  603. pf = target + pf;
  604. File.Copy(f, pf, true);
  605. }
  606. }
  607. }
  608. /// <summary>
  609. /// 获取文件md5
  610. /// </summary>
  611. /// <param name="file"></param>
  612. /// <returns></returns>
  613. public static string GetFileMD5(string file)
  614. {
  615. string result = string.Empty;
  616. if (File.Exists(file))
  617. {
  618. MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();
  619. using (FileStream fs = File.OpenRead(file))
  620. {
  621. result = BitConverter.ToString(m.ComputeHash(fs));
  622. }
  623. }
  624. return result;
  625. }
  626. }
  627. }