ComputerUniqueHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace efunbox_xyyf_windows.util
  8. {
  9. public class ComputerUniqueHelper
  10. {
  11. public static string GetComputerUUID()
  12. {
  13. var uuid = GetSmBIOSUUID();
  14. if (string.IsNullOrWhiteSpace(uuid))
  15. {
  16. var cpuID = GetCPUID();
  17. var biosSerialNumber = GetBIOSSerialNumber();
  18. var diskSerialNumber = GetDiskDriveSerialNumber();
  19. uuid = $"{cpuID}__{biosSerialNumber}__{diskSerialNumber}";
  20. }
  21. return uuid;
  22. }
  23. private static string GetSmBIOSUUID()
  24. {
  25. var cmd = "wmic csproduct get UUID";
  26. return ExecuteCMD(cmd, output =>
  27. {
  28. string uuid = GetTextAfterSpecialText(output, "UUID");
  29. if (uuid == "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF")
  30. {
  31. uuid = null;
  32. }
  33. return uuid;
  34. });
  35. }
  36. private static string GetCPUID()
  37. {
  38. var cmd = "wmic cpu get processorid";
  39. return ExecuteCMD(cmd, output =>
  40. {
  41. var cpuid = GetTextAfterSpecialText(output, "ProcessorId");
  42. return cpuid;
  43. });
  44. }
  45. private static string GetBIOSSerialNumber()
  46. {
  47. var cmd = "wmic bios get serialnumber";
  48. return ExecuteCMD(cmd, output =>
  49. {
  50. var serialNumber = GetTextAfterSpecialText(output, "SerialNumber");
  51. return serialNumber;
  52. });
  53. }
  54. private static string GetDiskDriveSerialNumber()
  55. {
  56. var cmd = "wmic diskdrive get serialnumber";
  57. return ExecuteCMD(cmd, output =>
  58. {
  59. var serialNumber = GetTextAfterSpecialText(output, "SerialNumber");
  60. return serialNumber;
  61. });
  62. }
  63. private static string GetTextAfterSpecialText(string fullText, string specialText)
  64. {
  65. if (string.IsNullOrWhiteSpace(fullText) || string.IsNullOrWhiteSpace(specialText))
  66. {
  67. return null;
  68. }
  69. string lastText = null;
  70. var idx = fullText.LastIndexOf(specialText);
  71. if (idx > 0)
  72. {
  73. lastText = fullText.Substring(idx + specialText.Length).Trim();
  74. }
  75. return lastText;
  76. }
  77. private static string ExecuteCMD(string cmd, Func<string, string> filterFunc)
  78. {
  79. var process = new Process();
  80. process.StartInfo.FileName = "cmd.exe";
  81. process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
  82. process.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
  83. process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
  84. process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
  85. process.StartInfo.CreateNoWindow = true;//不显示程序窗口
  86. process.Start();//启动程序
  87. process.StandardInput.WriteLine(cmd + " &exit");
  88. process.StandardInput.AutoFlush = true;
  89. //获取cmd窗口的输出信息
  90. var output = process.StandardOutput.ReadToEnd();
  91. process.WaitForExit();
  92. process.Close();
  93. return filterFunc(output);
  94. }
  95. }
  96. }