【C#】 获取计算机的硬件信息

添加引用:System.Management

  1  /// 
  2         /// 获取本机的MAC地址
  3         /// 
  4         /// 
  5         public static string GetMACString()
  6         {
  7             ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
  8             ManagementObjectCollection mo = mAdapter.GetInstances();
  9             foreach (ManagementBaseObject m in mo)
 10             {
 11                 if ((bool)m["IpEnabled"] == true)
 12                 {
 13                     return m["MacAddress"].ToString();
 14                 }
 15             }
 16             mo.Dispose();
 17             return null;
 18         }
 19 
 20 
 21 
 22         /// 
 23         /// 获取CPU的个数
 24         /// 
 25         /// 
 26         public static int GetCpuCount()
 27         {
 28             try
 29             {
 30                 using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
 31                 {
 32                     ManagementObjectCollection cpus = mCpu.GetInstances();
 33                     return cpus.Count;
 34                 }
 35             }
 36             catch
 37             {
 38             }
 39             return -1;
 40         }
 41 
 42 
 43 
 44         /// 
 45         /// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核
 46         /// 
 47         /// 
 48         public static string[] GetCpuMHZ()
 49         {
 50             ManagementClass mc = new ManagementClass("Win32_Processor");
 51             ManagementObjectCollection cpus = mc.GetInstances();
 52 
 53             string[] mHz = new string[cpus.Count];
 54             int c = 0;
 55             ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
 56             foreach (ManagementObject mo in mySearch.Get())
 57             {
 58                 mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
 59                 c++;
 60             }
 61             mc.Dispose();
 62             mySearch.Dispose();
 63             return mHz;
 64         }
 65 
 66 
 67 
 68         /// 
 69         /// 获取硬盘的大小
 70         /// 
 71         /// 
 72         public static string GetSizeOfDisk()
 73         {
 74             ManagementClass mc = new ManagementClass("Win32_DiskDrive");
 75             ManagementObjectCollection moj = mc.GetInstances();
 76             foreach (ManagementObject m in moj)
 77             {
 78                 return m.Properties["Size"].Value.ToString();
 79             }
 80             return "-1";
 81         }
 82 
 83 
 84 
 85 
 86         /// 
 87         /// 获取内存的大小
 88         /// 
 89         /// 
 90         public static string GetSizeOfMemery()
 91         {
 92             ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
 93             ManagementObjectCollection moc = mc.GetInstances();
 94 
 95             double sizeAll = 0.0;
 96             foreach (ManagementObject m in moc)
 97             {
 98                 if (m.Properties["TotalVisibleMemorySize"].Value != null)
 99                 {
100                     sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString());
101                 }
102             }
103             mc = null;
104             moc.Dispose();
105 
106             return sizeAll.ToString();
107         }
108 
109         /// 
110         /// 取CPU编号
111         /// 
112         /// 
113         public static String GetCpuID()
114         {
115             try
116             {
117                 ManagementClass mc = new ManagementClass("Win32_Processor");
118                 ManagementObjectCollection moc = mc.GetInstances();
119 
120                 String strCpuID = null;
121                 foreach (ManagementObject mo in moc)
122                 {
123                     strCpuID = mo.Properties["ProcessorId"].Value.ToString();
124                     break;
125                 }
126                 return strCpuID;
127             }
128             catch
129             {
130                 return "";
131             }
132 
133         } 

 

你可能感兴趣的:(【C#】 获取计算机的硬件信息)