PubMethod.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. using SAGA.DotNetUtils.Extend;
  2. namespace SAGA.DotNetUtils
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Imaging;
  10. using System.IO;
  11. using System.Management;
  12. using System.Net;
  13. using System.Net.NetworkInformation;
  14. using System.Net.Sockets;
  15. using System.Runtime.InteropServices;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Windows.Forms;
  19. public class PubMethod : StringOperate
  20. {
  21. public static string Combine(string strSavePath, string strFileName)
  22. {
  23. if (strSavePath.IsNullOrEmptyExt())
  24. {
  25. throw new ArgumentNullException("strSavePath");
  26. }
  27. if (strFileName.IsNullOrEmptyExt())
  28. {
  29. throw new ArgumentNullException("strFileName");
  30. }
  31. if (!strSavePath.EndsWith(@"\"))
  32. {
  33. strSavePath = strSavePath + @"\";
  34. }
  35. if (strFileName.StartsWith(@"\"))
  36. {
  37. strFileName = strFileName.TrimStart(new char[] { '\\' });
  38. }
  39. return (strSavePath + strFileName);
  40. }
  41. public static bool CompareBytes(byte[] buffer1, byte[] buffer2)
  42. {
  43. if ((buffer1 != null) || (buffer2 != null))
  44. {
  45. if ((buffer1 == null) || (buffer2 == null))
  46. {
  47. return false;
  48. }
  49. if (buffer1.Length != buffer2.Length)
  50. {
  51. return false;
  52. }
  53. for (int i = 0; i < buffer1.Length; i++)
  54. {
  55. if (buffer1[i] != buffer2[i])
  56. {
  57. return false;
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. public static int CompareDate(DateTime beginDt, DateTime endDt)
  64. {
  65. if (beginDt.Year > endDt.Year)
  66. {
  67. return 1;
  68. }
  69. if (beginDt.Year < endDt.Year)
  70. {
  71. return -1;
  72. }
  73. if (beginDt.Month > endDt.Month)
  74. {
  75. return 1;
  76. }
  77. if (beginDt.Month < endDt.Month)
  78. {
  79. return -1;
  80. }
  81. if (beginDt.Day > endDt.Day)
  82. {
  83. return 1;
  84. }
  85. if (beginDt.Day < endDt.Day)
  86. {
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. public static bool CompareFile(string strFile1, string strFile2)
  92. {
  93. FileInfo info = new FileInfo(strFile1);
  94. FileInfo info2 = new FileInfo(strFile2);
  95. if (!info.Exists || !info2.Exists)
  96. {
  97. return false;
  98. }
  99. if (info.Length != info2.Length)
  100. {
  101. return false;
  102. }
  103. try
  104. {
  105. info.OpenRead();
  106. info2.OpenRead();
  107. }
  108. catch (Exception)
  109. {
  110. return false;
  111. }
  112. using (FileStream stream = info.OpenRead())
  113. {
  114. using (FileStream stream2 = info2.OpenRead())
  115. {
  116. for (int i = 0; i < stream.Length; i++)
  117. {
  118. int num2 = stream.ReadByte();
  119. int num3 = stream2.ReadByte();
  120. if (num2 != num3)
  121. {
  122. return false;
  123. }
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. public static int CompareTime(DateTime beginDt, DateTime endDt)
  130. {
  131. int num = CompareDate(beginDt, endDt);
  132. if (num != 0)
  133. {
  134. return num;
  135. }
  136. if (beginDt.Hour > endDt.Hour)
  137. {
  138. return 1;
  139. }
  140. if (beginDt.Hour < endDt.Hour)
  141. {
  142. return -1;
  143. }
  144. if (beginDt.Minute > endDt.Minute)
  145. {
  146. return 1;
  147. }
  148. if (beginDt.Minute < endDt.Minute)
  149. {
  150. return -1;
  151. }
  152. if (beginDt.Second > endDt.Second)
  153. {
  154. return 1;
  155. }
  156. if (beginDt.Second < endDt.Second)
  157. {
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. public static string CreateGuidKey()
  163. {
  164. DateTime now = DateTime.Now;
  165. string str = now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0');
  166. string str2 = Guid.NewGuid().ToString().Replace("-", string.Empty).ToUpper();
  167. System.Random random = new System.Random(Guid.NewGuid().GetHashCode());
  168. string str3 = random.Next(1, 0x270f).ToString().PadLeft(4, '0');
  169. return (str + "_" + str2 + "_" + str3);
  170. }
  171. public static string CreateNowTimeString()
  172. {
  173. DateTime now = DateTime.Now;
  174. return (now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0'));
  175. }
  176. public static string CreateUniqueName()
  177. {
  178. DateTime now = DateTime.Now;
  179. string str = now.Year.ToString().PadLeft(4, '0') + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0') + now.Millisecond.ToString().PadLeft(3, '0');
  180. System.Random random = new System.Random(Guid.NewGuid().GetHashCode());
  181. string str2 = random.Next(1, 0x270f).ToString().PadLeft(4, '0');
  182. return (str + "_" + str2);
  183. }
  184. public static bool DeleteFile(string strFileName)
  185. {
  186. try
  187. {
  188. if (System.IO.File.Exists(strFileName))
  189. {
  190. FileAttribute attribute = new FileAttribute(strFileName) {
  191. IsReadOnly = false
  192. };
  193. System.IO.File.Delete(strFileName);
  194. return true;
  195. }
  196. return false;
  197. }
  198. catch
  199. {
  200. return false;
  201. }
  202. }
  203. public static void DeleteFolder(string folderName)
  204. {
  205. if (Directory.Exists(folderName))
  206. {
  207. DirectoryInfo info = new DirectoryInfo(folderName);
  208. foreach (FileInfo info2 in info.GetFiles())
  209. {
  210. try
  211. {
  212. FileAttribute attribute = new FileAttribute(info2.FullName);
  213. if (attribute.IsReadOnly)
  214. {
  215. attribute.IsReadOnly = false;
  216. }
  217. System.IO.File.Delete(info2.FullName);
  218. }
  219. catch
  220. {
  221. }
  222. }
  223. if (Directory.Exists(folderName))
  224. {
  225. foreach (DirectoryInfo info3 in info.GetDirectories())
  226. {
  227. DeleteFolder(info3.FullName);
  228. }
  229. try
  230. {
  231. Directory.Delete(info.FullName, true);
  232. }
  233. catch
  234. {
  235. }
  236. }
  237. }
  238. }
  239. public static bool EnumExistsValue(System.Enum source, System.Enum value)
  240. {
  241. string[] strArray = source.ToString().Trim().Split(new char[] { ',' });
  242. string str2 = value.ToString().Trim();
  243. for (int i = 0; i < strArray.Length; i++)
  244. {
  245. if (str2 == strArray[i].Trim())
  246. {
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. public static void ExtendLastCol(DataGridView grid)
  253. {
  254. if (grid.Columns.Count != 0)
  255. {
  256. int num = 0;
  257. foreach (DataGridViewColumn column in grid.Columns)
  258. {
  259. num += column.Width;
  260. }
  261. if (grid.Width > num)
  262. {
  263. DataGridViewColumn column2 = grid.Columns[grid.Columns.Count - 1];
  264. column2.Width = ((grid.Width - num) + column2.Width) - grid.RowHeadersWidth;
  265. }
  266. }
  267. }
  268. public static bool FileCopyNew(string strSourceFile, string strDestFile)
  269. {
  270. bool flag = true;
  271. if (!System.IO.File.Exists(strDestFile))
  272. {
  273. System.IO.File.Copy(strSourceFile, strDestFile, true);
  274. return flag;
  275. }
  276. FileInfo info = new FileInfo(strSourceFile);
  277. FileInfo info2 = new FileInfo(strDestFile);
  278. if (info.LastWriteTime > info2.LastWriteTime)
  279. {
  280. try
  281. {
  282. FileAttribute attribute = new FileAttribute(strDestFile) {
  283. IsReadOnly = false
  284. };
  285. System.IO.File.Delete(strDestFile);
  286. }
  287. catch
  288. {
  289. flag = false;
  290. }
  291. try
  292. {
  293. System.IO.File.Copy(strSourceFile, strDestFile, true);
  294. }
  295. catch
  296. {
  297. flag = false;
  298. }
  299. }
  300. return flag;
  301. }
  302. public static string GetAcadVersion(string strDwgFileName)
  303. {
  304. if (!System.IO.File.Exists(strDwgFileName))
  305. {
  306. return "";
  307. }
  308. FileInfo info = new FileInfo(strDwgFileName);
  309. if (info.Extension.ToLower() != ".dwg")
  310. {
  311. return "";
  312. }
  313. FileStream stream = new FileStream(strDwgFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  314. byte[] buffer = new byte[6];
  315. stream.Read(buffer, 0, buffer.Length);
  316. string str = new UTF8Encoding(true).GetString(buffer);
  317. stream.Close();
  318. return str.Substring(4, 2);
  319. }
  320. public static string GetCnWeek(string strValue)
  321. {
  322. string str = string.Empty;
  323. switch (strValue.Trim().ToLower())
  324. {
  325. case "monday":
  326. return "星期一";
  327. case "tuesday":
  328. return "星期二";
  329. case "wednesday":
  330. return "星期三";
  331. case "thursday":
  332. return "星期四";
  333. case "friday":
  334. return "星期五";
  335. case "saturday":
  336. return "星期六";
  337. case "sunday":
  338. return "星期日";
  339. }
  340. return str;
  341. }
  342. public static string GetCpuID()
  343. {
  344. try
  345. {
  346. ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
  347. string str = null;
  348. foreach (ManagementObject obj2 in instances)
  349. {
  350. str = obj2.Properties["ProcessorId"].Value.ToString().Trim();
  351. if (str.Length > 0)
  352. {
  353. break;
  354. }
  355. }
  356. return str;
  357. }
  358. catch
  359. {
  360. return string.Empty;
  361. }
  362. }
  363. public static DateTime? GetDateTime(object value)
  364. {
  365. if (value != null)
  366. {
  367. DateTime time;
  368. if (value == DBNull.Value)
  369. {
  370. return null;
  371. }
  372. string s = value.ToString().Trim();
  373. if (s.Length <= 0)
  374. {
  375. return null;
  376. }
  377. if (DateTime.TryParse(s, out time))
  378. {
  379. return new DateTime?(time);
  380. }
  381. }
  382. return null;
  383. }
  384. public static long GetDriveFreeSpace(string strDrive)
  385. {
  386. long lpFreeBytesAvalibe = 0L;
  387. long lpTotalNumberOfBytes = 0L;
  388. long lpTotalNumberOfFreeBytes = 0L;
  389. Win32APIMethod.GetDiskFreeSpaceEx(strDrive, out lpFreeBytesAvalibe, out lpTotalNumberOfBytes, out lpTotalNumberOfFreeBytes);
  390. return lpFreeBytesAvalibe;
  391. }
  392. public static string GetDriveFreeSpaceFormat(string strDrive)
  393. {
  394. double num2 = (((double) GetDriveFreeSpace(strDrive)) / 1024.0) / 1024.0;
  395. if (num2 < 1024.0)
  396. {
  397. return (Math.Round(num2, 2) + " MB");
  398. }
  399. return (Math.Round((double) (num2 / 1024.0), 3) + " GB");
  400. }
  401. public static int GetDriveType(string driveName)
  402. {
  403. return Win32APIMethod.GetDriveType(driveName);
  404. }
  405. public static Icon GetFileIcon(string strFileName, bool blnSmallIcon)
  406. {
  407. strFileName = Path.GetExtension(strFileName);
  408. int num = 0;
  409. Icon icon = null;
  410. Win32APIMethod.SHFILEINFO psfi = new Win32APIMethod.SHFILEINFO();
  411. if (blnSmallIcon)
  412. {
  413. num = (int) Win32APIMethod.SHGetFileInfo(strFileName, 0, ref psfi, (uint) Marshal.SizeOf(psfi), 0x111);
  414. }
  415. else
  416. {
  417. num = (int) Win32APIMethod.SHGetFileInfo(strFileName, 0, ref psfi, (uint) Marshal.SizeOf(psfi), 0x110);
  418. }
  419. if ((num > 0) && (((int) psfi.hIcon) != 0))
  420. {
  421. icon = Icon.FromHandle(psfi.hIcon);
  422. }
  423. return icon;
  424. }
  425. public static bool GetFileThumbnailImage(string strInFile, out string strOutFile)
  426. {
  427. strOutFile = string.Empty;
  428. try
  429. {
  430. Path.GetExtension(strInFile).ToLower().TrimStart(new char[] { '.' });
  431. if (!true)
  432. {
  433. return false;
  434. }
  435. strOutFile = Path.GetDirectoryName(strInFile) + @"\" + Guid.NewGuid().ToString();
  436. int width = 0;
  437. int height = 0;
  438. int num3 = 800;
  439. Image image = null;
  440. image = new Bitmap(strInFile);
  441. if ((image.Width <= num3) && (image.Height <= num3))
  442. {
  443. image.Save(strOutFile, ImageFormat.Jpeg);
  444. image.Dispose();
  445. return true;
  446. }
  447. if (image.Width > image.Height)
  448. {
  449. if (image.Width > (num3 - 2))
  450. {
  451. width = num3 - 2;
  452. height = (image.Height * (num3 - 2)) / image.Width;
  453. }
  454. else
  455. {
  456. width = image.Width;
  457. height = image.Height;
  458. }
  459. }
  460. else if (image.Height > (num3 - 2))
  461. {
  462. width = (image.Width * (num3 - 2)) / image.Height;
  463. height = num3 - 2;
  464. }
  465. else
  466. {
  467. width = image.Width;
  468. height = image.Height;
  469. }
  470. if (width == 0)
  471. {
  472. width = image.Width;
  473. }
  474. if (height == 0)
  475. {
  476. height = image.Height;
  477. }
  478. Bitmap bitmap = new Bitmap(width, height);
  479. try
  480. {
  481. Graphics graphics = Graphics.FromImage(bitmap);
  482. graphics.Clear(Color.White);
  483. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  484. graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
  485. graphics.Dispose();
  486. }
  487. catch
  488. {
  489. bitmap = new Bitmap(image.GetThumbnailImage(width, height, null, IntPtr.Zero));
  490. }
  491. image.Dispose();
  492. bitmap.Save(strOutFile, ImageFormat.Jpeg);
  493. }
  494. catch
  495. {
  496. return false;
  497. }
  498. return true;
  499. }
  500. public static string[] GetFontFamilyNames()
  501. {
  502. FontFamily[] fontFamilys = GetFontFamilys();
  503. string[] strArray = new string[fontFamilys.Length];
  504. for (int i = 0; i < fontFamilys.Length; i++)
  505. {
  506. strArray[i] = fontFamilys[i].Name;
  507. }
  508. return strArray;
  509. }
  510. public static FontFamily[] GetFontFamilys()
  511. {
  512. return FontFamily.Families;
  513. }
  514. public static string GetHostAndIp(string IPorHostName, out string HostName)
  515. {
  516. string str = string.Empty;
  517. string str2 = string.Empty;
  518. try
  519. {
  520. IPHostEntry hostEntry = Dns.GetHostEntry(IPorHostName);
  521. if (!StringOperate.CheckIP(IPorHostName))
  522. {
  523. str2 = IPorHostName;
  524. foreach (IPAddress address in hostEntry.AddressList)
  525. {
  526. if (StringOperate.CheckIP(address.ToString().Trim()))
  527. {
  528. str = address.ToString().Trim();
  529. goto Label_00C1;
  530. }
  531. }
  532. }
  533. else
  534. {
  535. str = IPorHostName;
  536. str2 = hostEntry.HostName.ToLower();
  537. if (str2.IndexOf(".") >= 0)
  538. {
  539. str2 = str2.Substring(0, str2.IndexOf("."));
  540. }
  541. }
  542. }
  543. catch (SocketException exception)
  544. {
  545. if (exception.ToString().IndexOf("不知道这样的主机") >= 0)
  546. {
  547. str = string.Empty;
  548. str2 = string.Empty;
  549. }
  550. }
  551. Label_00C1:
  552. HostName = str2;
  553. return str;
  554. }
  555. public static int? GetInteger(object value)
  556. {
  557. if (value != null)
  558. {
  559. int num;
  560. if (value == DBNull.Value)
  561. {
  562. return null;
  563. }
  564. string s = value.ToString().Trim();
  565. if (s.Length <= 0)
  566. {
  567. return null;
  568. }
  569. if (int.TryParse(s, out num))
  570. {
  571. return new int?(num);
  572. }
  573. }
  574. return null;
  575. }
  576. public static string GetMachineIP()
  577. {
  578. return GetMachineIP(Environment.MachineName);
  579. }
  580. public static string GetMachineIP(string MachineName)
  581. {
  582. string str = string.Empty;
  583. if (MachineName != string.Empty)
  584. {
  585. try
  586. {
  587. foreach (IPAddress address in Dns.GetHostEntry(MachineName).AddressList)
  588. {
  589. if (StringOperate.CheckIP(address.ToString().Trim()))
  590. {
  591. return address.ToString().Trim();
  592. }
  593. }
  594. }
  595. catch
  596. {
  597. }
  598. }
  599. return str;
  600. }
  601. public static string GetNetCardMAC()
  602. {
  603. try
  604. {
  605. string str = string.Empty;
  606. ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
  607. foreach (ManagementObject obj2 in class2.GetInstances())
  608. {
  609. if ((bool) obj2["IPEnabled"])
  610. {
  611. str = obj2["MACAddress"].ToString().Trim();
  612. if (str.Length > 0)
  613. {
  614. return str;
  615. }
  616. }
  617. }
  618. return str;
  619. }
  620. catch
  621. {
  622. return string.Empty;
  623. }
  624. }
  625. public static double? GetNumber(object value)
  626. {
  627. if (value != null)
  628. {
  629. double num;
  630. if (value == DBNull.Value)
  631. {
  632. return null;
  633. }
  634. string s = value.ToString().Trim();
  635. if (s.Length <= 0)
  636. {
  637. return null;
  638. }
  639. if (double.TryParse(s, out num))
  640. {
  641. return new double?(num);
  642. }
  643. }
  644. return null;
  645. }
  646. public static Image GetThubmnailImage(Image image, int intImageSize)
  647. {
  648. if (intImageSize == 0)
  649. {
  650. intImageSize = 200;
  651. }
  652. int width = 0;
  653. int height = 0;
  654. if (image.Width > image.Height)
  655. {
  656. if (image.Width > (intImageSize - 2))
  657. {
  658. width = intImageSize - 2;
  659. height = (image.Height * (intImageSize - 2)) / image.Width;
  660. }
  661. else
  662. {
  663. width = image.Width;
  664. height = image.Height;
  665. }
  666. }
  667. else if (image.Height > (intImageSize - 2))
  668. {
  669. width = (image.Width * (intImageSize - 2)) / image.Height;
  670. height = intImageSize - 2;
  671. }
  672. else
  673. {
  674. width = image.Width;
  675. height = image.Height;
  676. }
  677. if (width == 0)
  678. {
  679. width = image.Width;
  680. }
  681. if (height == 0)
  682. {
  683. height = image.Height;
  684. }
  685. Bitmap bitmap = new Bitmap(intImageSize, intImageSize);
  686. Graphics graphics = Graphics.FromImage(bitmap);
  687. graphics.Clear(Color.White);
  688. Pen pen = new Pen(Color.Black, 1f);
  689. graphics.DrawRectangle(pen, 0, 0, intImageSize - 1, intImageSize - 1);
  690. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  691. graphics.DrawImage(image, new Rectangle((intImageSize - width) / 2, (intImageSize - height) / 2, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
  692. graphics.Dispose();
  693. image.Dispose();
  694. return bitmap;
  695. }
  696. public static List<T[]> GroupArray<T>(T[] source, int intGroups)
  697. {
  698. List<T[]> list = new List<T[]>();
  699. if (source.Length < intGroups)
  700. {
  701. list.Add(source);
  702. return list;
  703. }
  704. int num = source.Length / intGroups;
  705. for (int i = 0; i < intGroups; i++)
  706. {
  707. List<T> list2 = new List<T>();
  708. int num3 = num;
  709. if (i == (intGroups - 1))
  710. {
  711. num3 += source.Length % intGroups;
  712. }
  713. for (int j = 0; j < num3; j++)
  714. {
  715. list2.Add(source[(i * num) + j]);
  716. }
  717. list.Add(list2.ToArray());
  718. }
  719. return list;
  720. }
  721. public static void KillProcess(string ProName)
  722. {
  723. try
  724. {
  725. Process[] processesByName = Process.GetProcessesByName(ProName);
  726. if (processesByName.Length > 0)
  727. {
  728. for (int i = 0; i < processesByName.Length; i++)
  729. {
  730. processesByName[i].Kill();
  731. Thread.Sleep(0x3e8);
  732. }
  733. }
  734. }
  735. catch
  736. {
  737. }
  738. }
  739. public static bool KillProcess(string ProName, int intCpuMinutes)
  740. {
  741. bool flag = false;
  742. try
  743. {
  744. Process[] processesByName = Process.GetProcessesByName(ProName);
  745. if (processesByName.Length <= 0)
  746. {
  747. return flag;
  748. }
  749. for (int i = 0; i < processesByName.Length; i++)
  750. {
  751. Process process = processesByName[i];
  752. if (process.TotalProcessorTime.Minutes == intCpuMinutes)
  753. {
  754. process.Kill();
  755. flag = true;
  756. }
  757. }
  758. }
  759. catch
  760. {
  761. }
  762. return flag;
  763. }
  764. public static bool KillProcess(string ProName, long Memory)
  765. {
  766. bool flag = false;
  767. try
  768. {
  769. Process[] processesByName = Process.GetProcessesByName(ProName);
  770. if (processesByName.Length <= 0)
  771. {
  772. return flag;
  773. }
  774. for (int i = 0; i < processesByName.Length; i++)
  775. {
  776. Process process = processesByName[i];
  777. if (process.WorkingSet64 > Memory)
  778. {
  779. SystemLogger.Default.Write(" Cur Memory Size : " + process.WorkingSet64.ToString());
  780. process.Kill();
  781. flag = true;
  782. }
  783. }
  784. }
  785. catch
  786. {
  787. }
  788. return flag;
  789. }
  790. public static void NavigateFile(string strFileName)
  791. {
  792. Process.Start("explorer.exe", "/select," + strFileName);
  793. }
  794. public static int Random(int intMaxValue)
  795. {
  796. return new System.Random(Guid.NewGuid().GetHashCode()).Next(-2147483648, intMaxValue);
  797. }
  798. public static int Random(int intMinValue, int intMaxValue)
  799. {
  800. return new System.Random(Guid.NewGuid().GetHashCode()).Next(intMinValue, intMaxValue);
  801. }
  802. public static string RemoveEmptyZero(string s)
  803. {
  804. if (s == null)
  805. {
  806. return s;
  807. }
  808. int index = s.IndexOf(".");
  809. if (index <= -1)
  810. {
  811. return s;
  812. }
  813. List<char> list = new List<char>();
  814. string str = s.Substring(index);
  815. for (int i = 0; i < str.Length; i++)
  816. {
  817. char c = str[i];
  818. if (char.IsNumber(c))
  819. {
  820. string str2 = str.Substring(i);
  821. bool flag = false;
  822. for (int j = 1; j <= 9; j++)
  823. {
  824. if (str2.IndexOf(j.ToString()) > -1)
  825. {
  826. flag = true;
  827. break;
  828. }
  829. }
  830. if (flag)
  831. {
  832. list.Add(c);
  833. }
  834. continue;
  835. }
  836. if (c == '.')
  837. {
  838. string str3 = str.Substring(i);
  839. bool flag2 = false;
  840. for (int k = 1; k <= 9; k++)
  841. {
  842. if (str3.IndexOf(k.ToString()) > -1)
  843. {
  844. flag2 = true;
  845. break;
  846. }
  847. }
  848. if (flag2)
  849. {
  850. list.Add(c);
  851. }
  852. continue;
  853. }
  854. list.Add(c);
  855. }
  856. if (list.Count > 0)
  857. {
  858. return (s.Substring(0, index) + new string(list.ToArray()));
  859. }
  860. return s.Substring(0, index);
  861. }
  862. public static string ReplaceErrChar(object objValue)
  863. {
  864. return objValue.ToString().Replace(@"\", string.Empty).Replace("'", string.Empty);
  865. }
  866. public static string ReplaceErrChar(string strValue)
  867. {
  868. return strValue.Replace(@"\", string.Empty).Replace("'", string.Empty);
  869. }
  870. public static List<T[]> SplitArray<T>(T[] source, int intFixLength)
  871. {
  872. List<T[]> list = new List<T[]>();
  873. List<T> list2 = new List<T>();
  874. for (int i = 0; i < source.Length; i++)
  875. {
  876. list2.Add(source[i]);
  877. if ((list2.Count >= intFixLength) || (i == (source.Length - 1)))
  878. {
  879. list.Add(list2.ToArray());
  880. list2.Clear();
  881. }
  882. }
  883. return list;
  884. }
  885. public static void SwapValue<T>(ref T a, ref T b)
  886. {
  887. T local = a;
  888. a = b;
  889. b = local;
  890. }
  891. public static bool TestMachineOnline(string MachineIP)
  892. {
  893. try
  894. {
  895. string hostNameOrAddress = MachineIP;
  896. if (hostNameOrAddress.IndexOf('\\') > 0)
  897. {
  898. hostNameOrAddress = hostNameOrAddress.Substring(0, hostNameOrAddress.IndexOf('\\'));
  899. }
  900. Ping ping = new Ping();
  901. if (ping.Send(hostNameOrAddress, 0x7d0).Status == IPStatus.Success)
  902. {
  903. return true;
  904. }
  905. }
  906. catch (Exception exception)
  907. {
  908. SystemLogger.Default.Write("Ping " + MachineIP + exception.ToString());
  909. return true;
  910. }
  911. SystemLogger.Default.Write("现在不能ping通机器:" + MachineIP);
  912. return false;
  913. }
  914. public static string[] ToStringArray<T>(T[] items)
  915. {
  916. string[] strArray = new string[items.Length];
  917. for (int i = 0; i < items.Length; i++)
  918. {
  919. if (items[i] == null)
  920. {
  921. strArray[i] = string.Empty;
  922. }
  923. else
  924. {
  925. strArray[i] = items[i].ToString();
  926. }
  927. }
  928. return strArray;
  929. }
  930. }
  931. }