unity3d 获取cpu序列号,硬盘ID,网卡MAC地址等硬件可用于加密

首先在添加引用中选中System.Management

  1. using System.Management;
  2. using System.Management.Instrumentation;
  3. private void GetInfo() 

  4. string cpuInfo = "";//cpu序列号 
  5. ManagementClass cimobject = new ManagementClass("Win32_Processor"); 
  6. ManagementObjectCollection moc = cimobject.GetInstances(); 
  7. foreach(ManagementObject mo in moc) 

  8. cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); 
  9. Response.Write ("cpu序列号:"+cpuInfo.ToString ()); 

  10. //获取硬盘ID 
  11. String HDid; 
  12. ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"); 
  13. ManagementObjectCollection moc1 = cimobject1.GetInstances(); 
  14. foreach(ManagementObject mo in moc1) 

  15. HDid = (string)mo.Properties["Model"].Value; 
  16. Response.Write ("硬盘序列号:"+HDid.ToString ()); 



  17. //获取网卡硬件地址 


  18. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
  19. ManagementObjectCollection moc2 = mc.GetInstances(); 
  20. foreach(ManagementObject mo in moc2) 

  21. if((bool)mo["IPEnabled"] == true) 
  22. Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString()); 
  23. mo.Dispose(); 

  24. }

  25. public static float GetCPUPersent()
  26. {
  27. float cpuload = 0;
  28. const string categoryname = "processor";
  29. const string countername = "% processor time";
  30. const string instancename = "_total";
  31. PerformanceCounter pc = new PerformanceCounter(categoryname, countername, instancename);
  32. int i = 10;
  33. while (i > 0)
  34. {
  35. Thread.Sleep(1000); // wait for 1 second 
  36. cpuload = pc.NextValue();
  37. if (cpuload > 0)
  38. {
  39. break;
  40. }
  41. i--;
  42. }
  43. return cpuload;
  44. }
  45. public static void GetDiskSpace(string path, out long DiskAll, out long DiskActive)
  46. {
  47. DiskAll = 0;
  48. DiskActive = 0;
  49. long a, b, c;
  50. int aaa = GetDiskFreeSpaceEx(path, out a, out b, out c);
  51. DiskAll = (long)(b / 1024 / 1024);
  52. DiskActive = (long)(a / 1024 / 1024);
  53. }
  54. public static void GetMemoryInfo(out uint MemoryAll, out uint MemoryUsed)
  55. {
  56. MemoryAll = 0;
  57. MemoryUsed = 0;
  58. MEMORY_INFO MemInfo = new MEMORY_INFO();
  59. GlobalMemoryStatus(ref MemInfo);
  60. MemoryAll = MemInfo.dwTotalPhys / 1024 / 1024;
  61. MemoryUsed = (MemInfo.dwTotalPhys - MemInfo.dwAvailPhys) / 1024 / 1024;
  62. }
复制代码
  1. [DllImport("kernel32")]
  2. public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
  3. //定义内存的信息结构 
  4. [StructLayout(LayoutKind.Sequential)]
  5. public struct MEMORY_INFO
  6. {
  7. public uint dwLength;
  8. public uint dwMemoryLoad;
  9. public uint dwTotalPhys;
  10. public uint dwAvailPhys;
  11. public uint dwTotalPageFile;
  12. public uint dwAvailPageFile;
  13. public uint dwTotalVirtual;
  14. public uint dwAvailVirtual;
  15. }
  16. [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")]
  17. public static extern int GetDiskFreeSpaceEx(string lpRootPathName, out long lpFreeBytesAvailable, out long lpTotalNumberOfBytes, out long lpTotalNumberOfFreeBytes);
复制代码




原文连接: http://cl314413.blog.163.com/blog/static/19050797620127302281752/

你可能感兴趣的:(unity3d 获取cpu序列号,硬盘ID,网卡MAC地址等硬件可用于加密)