革博士程序V1仓库
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. 
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Management;
  10. using System.Reflection;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace LeatherApp.Utils
  17. {
  18. public static class Util
  19. {
  20. private static Regex RegNumber = new Regex("^[0-9]+$"); //匹配 整数(不可以带正负号)
  21. private static Regex RegDecimal = new Regex("^(-?\\d+)(\\.\\d+)?$"); //浮点 (可以带正负号)
  22. /// <summary>
  23. /// 获取适合大小
  24. /// </summary>
  25. /// <param name="sourceSize"></param>
  26. /// <param name="targetSize"></param>
  27. /// <returns></returns>
  28. public static Size getNewSize(Size sourceSize, Size targetSize)
  29. {
  30. if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
  31. return sourceSize;
  32. int num = sourceSize.Width / targetSize.Width;
  33. int num2 = sourceSize.Height / targetSize.Height;
  34. int num3 = (num < num2) ? num2 : num;
  35. return new Size(sourceSize.Width / num3, sourceSize.Height / num3);
  36. }
  37. /// <summary>
  38. /// 等比计算宽高
  39. /// </summary>
  40. /// <param name="sourceSize"></param>
  41. /// <param name="targetSize"></param>
  42. /// <returns></returns>
  43. public static SizeF getNewSize(SizeF sourceSize, SizeF targetSize)
  44. {
  45. if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
  46. return sourceSize;
  47. float num = sourceSize.Width / targetSize.Width;
  48. float num2 = sourceSize.Height / targetSize.Height;
  49. float num3 = (num < num2) ? num2 : num;
  50. return new SizeF(sourceSize.Width / num3, sourceSize.Height / num3);
  51. }
  52. /// <summary>
  53. /// 写INI文件
  54. /// </summary>
  55. /// <param name="Section">Section</param>
  56. /// <param name="Key">Key</param>
  57. /// <param name="value">value</param>
  58. public static void WriteIniValue(string filePath, string Section, string Key, string value)
  59. {
  60. API.WritePrivateProfileString(Section, Key, value, filePath);
  61. }
  62. /// <summary>
  63. /// 读取INI文件指定部分
  64. /// </summary>
  65. /// <param name="Section">Section</param>
  66. /// <param name="Key">Key</param>
  67. /// <returns>String</returns>
  68. public static string ReadIniValue(string filePath, string Section, string Key)
  69. {
  70. StringBuilder temp = new StringBuilder(255);
  71. int i = API.GetPrivateProfileString(Section, Key, "", temp, 255, filePath);
  72. return temp.ToString().Trim();
  73. }
  74. /// <summary>
  75. /// 整数
  76. /// </summary>
  77. /// <param name="inputData"></param>
  78. /// <returns></returns>
  79. public static bool IsNumber(string inputData)
  80. {
  81. Match m = RegNumber.Match(inputData);
  82. return m.Success;
  83. }
  84. public static bool IsNumber(object inputData)
  85. {
  86. if (inputData == null) return false;
  87. Match m = RegNumber.Match(inputData.ToString());
  88. return m.Success;
  89. }
  90. public static bool IsDecimal(string inputData)
  91. {
  92. Match m = RegDecimal.Match(inputData);
  93. return m.Success;
  94. }
  95. public static bool IsDecimal(object inputData)
  96. {
  97. if (inputData == null) return false;
  98. Match m = RegDecimal.Match(inputData.ToString());
  99. return m.Success;
  100. }
  101. public static string file2Base64(string filePath)
  102. {
  103. return Convert.ToBase64String(File.ReadAllBytes(filePath));
  104. }
  105. /// <summary>
  106. /// 结构体转byte数组
  107. /// </summary>
  108. /// <param name="structObj">要转换的结构体</param>
  109. /// <returns>转换后的byte数组</returns>
  110. public static byte[] StructToBytes(object structObj)
  111. {
  112. int size = Marshal.SizeOf(structObj);
  113. IntPtr buffer = Marshal.AllocHGlobal(size);
  114. try
  115. {
  116. Marshal.StructureToPtr(structObj, buffer, false);
  117. byte[] bytes = new byte[size];
  118. Marshal.Copy(buffer, bytes, 0, size);
  119. return bytes;
  120. }
  121. finally
  122. {
  123. Marshal.FreeHGlobal(buffer);
  124. }
  125. }
  126. /// <summary>
  127. /// byte数组转结构体
  128. /// </summary>
  129. /// <param name="bytes">byte数组</param>
  130. /// <param name="type">结构体类型</param>
  131. /// <returns>转换后的结构体</returns>
  132. public static object BytesToStruct(byte[] bytes, Type strcutType)
  133. {
  134. int size = Marshal.SizeOf(strcutType);
  135. IntPtr buffer = Marshal.AllocHGlobal(size);
  136. try
  137. {
  138. Marshal.Copy(bytes, 0, buffer, size);
  139. return Marshal.PtrToStructure(buffer, strcutType);
  140. }
  141. finally
  142. {
  143. Marshal.FreeHGlobal(buffer);
  144. }
  145. }
  146. /// <summary>
  147. /// 是否为系统不可操作窗体(如桌面、任务栏等)
  148. /// </summary>
  149. /// <returns></returns>
  150. public static bool IsSysWin(IntPtr WinHwnd)
  151. {
  152. if (WinHwnd != IntPtr.Zero)
  153. {
  154. StringBuilder lsbClassName = new StringBuilder(256);
  155. API.GetClassName(WinHwnd, lsbClassName, 256 - 1);
  156. string lsWinClassName = lsbClassName.ToString().ToLower();
  157. return (lsWinClassName == "progman" || lsWinClassName == "shell_traywnd");
  158. }
  159. return false;
  160. }
  161. public static bool DisableDevice(string sDeviceID)
  162. {
  163. try
  164. {
  165. if (sDeviceID == null || sDeviceID.Trim() == "" || !System.IO.File.Exists(System.Environment.SystemDirectory + "\\devcon.exe"))
  166. return false;
  167. string[] lsDeviceIDs = sDeviceID.Split(new char[] { ';', ',' });
  168. ProcessStartInfo info;
  169. for (int i = 0; i < lsDeviceIDs.Length; i++)
  170. {
  171. if (lsDeviceIDs[i].Trim() == "") continue;
  172. info = new ProcessStartInfo();
  173. info.FileName = System.Environment.SystemDirectory + "\\devcon.exe"; // 要启动的程序
  174. info.Arguments = "disable " + lsDeviceIDs[i]; //传递给程序的参数
  175. info.WindowStyle = ProcessWindowStyle.Hidden; //隐藏窗口
  176. Process pro = Process.Start(info); //启动程序
  177. }
  178. return true;
  179. }
  180. catch
  181. {
  182. return false;
  183. }
  184. }
  185. public static bool EnableDevice(string sDeviceID)
  186. {
  187. try
  188. {
  189. if (sDeviceID == null || sDeviceID.Trim() == "" || !System.IO.File.Exists(System.Environment.SystemDirectory + "\\devcon.exe"))
  190. return false;
  191. string[] lsDeviceIDs = sDeviceID.Split(new char[] { ';' });
  192. ProcessStartInfo info;
  193. for (int i = 0; i < lsDeviceIDs.Length; i++)
  194. {
  195. if (lsDeviceIDs[i].Trim() == "") continue;
  196. info = new ProcessStartInfo();
  197. info.FileName = System.Environment.SystemDirectory + "\\devcon.exe"; // 要启动的程序
  198. info.Arguments = "enable " + lsDeviceIDs[i]; //传递给程序的参数
  199. info.WindowStyle = ProcessWindowStyle.Hidden; //隐藏窗口
  200. Process pro = Process.Start(info); //启动程序
  201. }
  202. return true;
  203. }
  204. catch
  205. {
  206. return false;
  207. }
  208. }
  209. /// <summary>
  210. /// 获取时间戳
  211. /// </summary>
  212. public static string GetTimestamp()
  213. {
  214. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  215. DateTime dtNow = DateTime.Now;
  216. TimeSpan toNow = dtNow.Subtract(dtStart);
  217. System.Random rand = new Random();
  218. string timeStamp = toNow.Ticks.ToString() + rand.Next(9);
  219. return timeStamp;
  220. }
  221. /// <summary>
  222. /// 获取MD5串(未转大小写)
  223. /// </summary>
  224. public static string GetMD5(string str)
  225. {
  226. return GetMD5(str, Encoding.UTF8);
  227. }
  228. public static string GetMD5(string str, Encoding ed)
  229. {
  230. byte[] data = ed.GetBytes(str);
  231. data = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(data);
  232. string ret = "";
  233. for (int i = 0; i < data.Length; i++)
  234. {
  235. ret += data[i].ToString("x1").PadLeft(2, '0');//ToString("x1"):转换为16进制
  236. }
  237. return ret;
  238. }
  239. /// <summary>
  240. /// 获取MD5串(未转大小写)
  241. /// </summary>
  242. public static string GetMD5(byte[] data)
  243. {
  244. data = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(data);
  245. string ret = "";
  246. for (int i = 0; i < data.Length; i++)
  247. {
  248. ret += data[i].ToString("x1").PadLeft(2, '0');//ToString("x1"):转换为16进制
  249. }
  250. return ret;
  251. }
  252. /// <summary>
  253. /// MD5 16位加密
  254. /// </summary>
  255. /// <param name="ConvertString"></param>
  256. /// <returns></returns>
  257. public static string GetMd5_16bit(string data)
  258. {
  259. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  260. string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(data)), 4, 8);
  261. t2 = t2.Replace("-", "");
  262. return t2;
  263. }
  264. public static string GetMd5_16bit(byte[] data)
  265. {
  266. System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  267. string t2 = BitConverter.ToString(md5.ComputeHash(data), 4, 8);
  268. t2 = t2.Replace("-", "");
  269. return t2;
  270. }
  271. public static string subString(string text, int leftLen)
  272. {
  273. if (text == null || text.Length < leftLen)
  274. return text;
  275. return text.Substring(0, leftLen) + "...";
  276. }
  277. //cpu序列号
  278. //获取计算机其它信息
  279. public static string GetPCOtherInfo()
  280. {
  281. string lsInfo;
  282. string lsName = System.Net.Dns.GetHostName();
  283. lsInfo = "计算机名称:" + lsName + " ";
  284. // 获得本机局域网IP地址
  285. System.Net.IPAddress addr = new System.Net.IPAddress(System.Net.Dns.GetHostByName(lsName).AddressList[0].Address);
  286. lsInfo += "局域网内IP地址:" + addr.ToString();
  287. //
  288. return lsInfo;
  289. }
  290. /// <summary>
  291. /// 模糊匹配字符串
  292. /// </summary>
  293. /// <param name="fullValue"></param>
  294. /// <param name="allKeys"></param>
  295. /// <param name="matchCase"></param>
  296. /// <param name="mustFirst"></param>
  297. /// <returns></returns>
  298. public static bool find_Fuzzy(string fullValue, List<string> allKeys, bool matchCase = true, bool mustFirst = true)
  299. {
  300. if (!matchCase)
  301. fullValue = fullValue.ToLower();
  302. string keyName = "";
  303. allKeys.ForEach(key => {
  304. int index = fullValue.IndexOf(matchCase ? key : key.ToLower());
  305. if (index > -1)
  306. {
  307. if (mustFirst)
  308. {
  309. if (index == 0)
  310. {
  311. keyName = key;
  312. return;
  313. }
  314. }
  315. else
  316. {
  317. keyName = key;
  318. return;
  319. }
  320. }
  321. });
  322. return keyName != "";
  323. }
  324. public static JObject getPropertys(Object o, string[] skipPropertys)
  325. {
  326. JObject data = new JObject();
  327. Type t = o.GetType();//获得该类的Type
  328. //再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了
  329. foreach (PropertyInfo pi in t.GetProperties())
  330. {
  331. object value1 = pi.GetValue(o, null);//用pi.GetValue获得值
  332. string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
  333. //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
  334. if (value1 == null)
  335. {
  336. data.Add(name, null);
  337. }
  338. else
  339. {
  340. Type type = value1.GetType();
  341. if (type == typeof(string) || type == typeof(int) || type == typeof(float) || type == typeof(double) || type == typeof(decimal))
  342. {
  343. data.Add(name, value1.ToString());
  344. }
  345. }
  346. }
  347. return data;
  348. }
  349. /// <summary>
  350. /// 获取文件对应MIME类型
  351. /// </summary>
  352. /// <param name="fileExtention">文件扩展名,如.jpg</param>
  353. /// <returns></returns>
  354. public static string GetContentType(string fileExtention)
  355. {
  356. if (string.Compare(fileExtention, ".html", true) == 0 || string.Compare(fileExtention, ".htm", true) == 0)
  357. return "text/html;charset=utf-8";
  358. else if (string.Compare(fileExtention, ".js", true) == 0)
  359. return "application/javascript";
  360. else if (string.Compare(fileExtention, ".css", true) == 0)
  361. return "text/css";
  362. else if (string.Compare(fileExtention, ".png", true) == 0)
  363. return "image/png";
  364. else if (string.Compare(fileExtention, ".jpg", true) == 0 || string.Compare(fileExtention, ".jpeg", true) == 0)
  365. return "image/jpeg";
  366. else if (string.Compare(fileExtention, ".gif", true) == 0)
  367. return "image/gif";
  368. else if (string.Compare(fileExtention, ".swf", true) == 0)
  369. return "application/x-shockwave-flash";
  370. else if (string.Compare(fileExtention, ".bcmap", true) == 0)
  371. return "image/svg+xml";
  372. else if (string.Compare(fileExtention, ".properties", true) == 0)
  373. return "application/octet-stream";
  374. else if (string.Compare(fileExtention, ".map", true) == 0)
  375. return "text/plain";
  376. else if (string.Compare(fileExtention, ".svg", true) == 0)
  377. return "image/svg+xml";
  378. else if (string.Compare(fileExtention, ".pdf", true) == 0)
  379. return "application/pdf";
  380. else if (string.Compare(fileExtention, ".txt", true) == 0)
  381. return "text/*";
  382. else if (string.Compare(fileExtention, ".dat", true) == 0)
  383. return "text/*";
  384. else
  385. return "application/octet-stream";//application/octet-stream
  386. }
  387. /// <summary>
  388. /// mm转像素
  389. /// </summary>
  390. /// <param name="mm"></param>
  391. /// <param name="dpi">PDF=72(默认) 显示器=96</param>
  392. /// <returns></returns>
  393. public static int mm2px(float mm, int dpi = 72)
  394. {
  395. return (int)((dpi / 25.4f) * mm);
  396. }
  397. /// <summary>
  398. /// 像素转mm
  399. /// </summary>
  400. /// <param name="px"></param>
  401. /// <param name="dpi">PDF=72(默认) 显示器=96</param>
  402. /// <returns></returns>
  403. public static float px2mm(int px, int dpi = 72)
  404. {
  405. return px / (dpi / 25.4f);
  406. }
  407. public static void addKey(JObject obj, string key, JToken value)
  408. {
  409. if (obj.ContainsKey(key))
  410. obj[key] = value;
  411. else
  412. obj.Add(key, value);
  413. }
  414. /// <summary>
  415. /// IO二进制数据格式化到8位 XXXX10X0
  416. /// </summary>
  417. /// <param name="datas"></param>
  418. /// <returns></returns>
  419. public static string[] IODataFormatBinaryStr(string[] datas, bool clone,char defaultPadChar='X')
  420. {
  421. string[] datas2= new string[datas.Length];
  422. for(int i = 0;i < datas.Length; i++)
  423. {
  424. if (clone)
  425. {
  426. datas2[i] = datas[i].Replace(" ", "");
  427. if (datas2[i].Length > 8)
  428. datas2[i] = datas2[i].Substring(datas2[i].Length - 8);
  429. else if (datas2[i].Length < 8)
  430. datas2[i] = datas2[i].PadLeft(8, defaultPadChar);
  431. }
  432. else
  433. {
  434. datas[i] = datas[i].Replace(" ", "");
  435. if (datas[i].Length > 8)
  436. datas[i] = datas[i].Substring(datas[i].Length - 8);
  437. else if (datas[i].Length < 8)
  438. datas[i] = datas[i].PadLeft(8, defaultPadChar);
  439. datas2 = datas;
  440. }
  441. }
  442. return datas2;
  443. }
  444. public static byte[] IOFormatBinary2bytes(string[] datas)
  445. {
  446. byte[] result=new byte[datas.Length];
  447. string str;
  448. for (int i = 0; i < datas.Length; i++)
  449. {
  450. str = "";
  451. datas[i] = datas[i].Replace(" ", "");
  452. for (int j = 0; j < datas[i].Length; j++)
  453. {
  454. if (datas[i][j] == '1' || datas[i][j] == 'H')
  455. str += "1";
  456. else
  457. str += "0";
  458. }
  459. result[i] = Convert.ToByte(str, 2);
  460. }
  461. return result;
  462. }
  463. public static void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
  464. {
  465. Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
  466. TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  467. }
  468. /// <summary>
  469. ///
  470. /// </summary>
  471. /// <param name="op_show_list">[XXHL XXXX,XXXX XXXX,...]</param>
  472. /// <param name="currIoDatas">[byte,byte,byte,byte]</param>
  473. /// <returns></returns>
  474. public static bool compareIOInput(string[] op_show_list, byte[] currIoDatas)
  475. {
  476. int isok = 0;//1-true 2-false
  477. string IN_OP_SHOW;
  478. for (int i = 0; i < currIoDatas.Length; i++)
  479. {
  480. IN_OP_SHOW = op_show_list[i].Replace(" ", "").PadLeft(8, 'X');
  481. if (IN_OP_SHOW.IndexOf('H') < 0 && IN_OP_SHOW.IndexOf('L') < 0)
  482. continue;
  483. for (int j = 7; j >= 0; j--)
  484. {
  485. byte bit = (byte)(1 << 7 - j);
  486. if (IN_OP_SHOW[j] == 'H')
  487. {
  488. if ((bit & currIoDatas[i]) > 0)
  489. isok = 1;
  490. else
  491. {
  492. isok = 2;
  493. break;
  494. }
  495. }
  496. else if (IN_OP_SHOW[j] == 'L')
  497. {
  498. if ((currIoDatas[i] ^ (currIoDatas[i] | bit)) > 0)
  499. isok = 1;
  500. else
  501. {
  502. isok = 2;
  503. break;
  504. }
  505. }
  506. }
  507. //已经不符
  508. if (isok == 2) break;
  509. }
  510. //
  511. return isok == 1;
  512. }
  513. public static string createSubDir(string basePath,List<string> subDirNames)
  514. {
  515. string path = basePath.Trim().Trim('\\')+"\\";
  516. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  517. foreach(string subDir in subDirNames)
  518. {
  519. path += subDir + "\\";
  520. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  521. }
  522. return path;
  523. }
  524. }
  525. }