C# 获取计算机信息

  1 //C#获取当前计算机的系统信息
  2 
  3 //系统标识符和版本号 
  4    string strSystem = Environment.OSVersion.ToString(); 
  5    //获取映射到进程上下文的物理内存量 
  6    string strRem=Environment.WorkingSet.ToString(); 
  7    //获取系统启动后经过的毫秒数 
  8    int iTC=Environment.TickCount/60000; 
  9    //系统目录的完全限定路径 
 10    string strSD=Environment.SystemDirectory; 
 11    //获取此本地计算机的 NetBIOS 名称 
 12    string strMN=Environment.MachineName; 
 13    //获取与当前用户关联的网络域名 
 14    string strUDN=Environment.UserDomainName; 
 15 
 16 
 17 ------------------------------------------------------------------------------------- 
 18    //输出所有驱动器号 
 19    string[] drv=System.IO.Directory.GetLogicalDrives(); 
 20 //还有文件信息,最后修改时间等等 
 21 
 22 //获取某一目录下的所有文件信息 
 23    //当前目录下的所有文件夹 
 24    DirectoryInfo di=new DirectoryInfo(strPath); 
 25    DirectoryInfo[] wjj=di.GetDirectories();
 26 
 27 //此代码从网络收集整理
 28 //经测试通过
 29 //需引用System.Management 
 30 using System;
 31 using System.Management;
 32 
 33 namespace MyCustomClassLib
 34 {
 35  /// <summary>
 36  /// 硬件信息类
 37  /// </summary>
 38  public class HardwareInfo
 39  {
 40   public HardwareInfo()
 41   {
 42    //
 43    // TODO: 在此处添加构造函数逻辑
 44    //
 45   }
 46 
 47   #region 硬件属性
 48   /// <summary>
 49   /// 机器名
 50   /// </summary>
 51   public string HostName
 52   {
 53    get{return System.NET.Dns.GetHostName();}   
 54   }
 55 
 56   /// <summary>
 57   /// CPU编号
 58   /// </summary>
 59   public string CPUID
 60   {
 61    get{return GetCpuID();}
 62   }
 63 
 64   /// <summary>
 65   /// 硬盘编号
 66   /// </summary>
 67   public string HardDiskID
 68   {
 69    get{return GetHardDiskID();}
 70   }
 71 
 72   /// <summary>
 73   /// 网卡MAC
 74   /// </summary>
 75   public string NetMac
 76   {
 77    get{return GetMac();}
 78   }
 79   
 80   #endregion
 81 
 82   #region 获取硬件信息的方法
 83   /// <summary>
 84   /// 获得CPU编号
 85   /// </summary>
 86   /// <returns></returns>
 87   private string GetCpuID()
 88   {
 89    string result="";
 90    try
 91    {
 92     ManagementClass mc = new ManagementClass("Win32_Processor");
 93     ManagementObjectCollection moc = mc.GetInstances();      
 94     
 95     foreach( ManagementObject mo in moc ) 
 96     {
 97      result=mo.Properties["ProcessorId"].Value.ToString();     
 98     }    
 99    }
100    catch
101    {
102     return "获取CPUID失败";
103    }
104    return result;
105   }
106 
107   /// <summary>
108   /// 获得硬盘编号
109   /// </summary>
110   /// <returns></returns>
111   private string GetHardDiskID()
112   {
113    string result="";
114    try
115    {
116     ManagementClass mcHD=new ManagementClass("win32_logicaldisk");
117     ManagementObjectCollection mocHD=mcHD.GetInstances();
118     foreach(ManagementObject m in mocHD)
119     {
120      if(m["DeviceID"].ToString()=="C:")
121      {
122       result=m["VolumeSerialNumber"].ToString().Trim();      
123      }
124     }
125    }
126    catch
127    {
128     return "获取硬盘ID失败";
129    }
130    return result;
131   }
132 
133   /// <summary>
134   /// 获得网卡MAC
135   /// </summary>
136   /// <returns></returns>
137   private string GetMac()
138   {
139    string result="";
140    try
141    {
142     ManagementClass mcMAC=new ManagementClass("Win32_NetworkAdapterConfiguration");
143     ManagementObjectCollection mocMAC=mcMAC.GetInstances();
144     foreach(ManagementObject m in mocMAC)
145     {
146      if((bool)m["IPEnabled"])
147      {
148       result=m["MacAddress"].ToString();     
149      }
150     }
151    }
152    catch
153    {
154     return "获取MAC失败";
155    } 
156    return result;
157   } 
158 
159   #endregion
160  }
161 } 
162 
163  
164 
165  
166 
167  
168 
169 //获取CPU编号:
170 view plaincopy to clipboardprint?
171 ManagementClass mc = new ManagementClass("Win32_Processor"); 
172 ManagementObjectCollection moc = mc.GetInstances(); 
173 string strID = null ; 
174 foreach( ManagementObject mo in moc ) 
175 { 
176 strID = mo.Properties["ProcessorId"].Value.ToString(); 
177 break; 
178 } 
179 textBox1.Text +=  "CPU ID:" + strID; 
180 ManagementClass mc = new ManagementClass("Win32_Processor");
181 ManagementObjectCollection moc = mc.GetInstances();
182 string strID = null ; 
183 foreach( ManagementObject mo in moc ) 
184 { 
185 strID = mo.Properties["ProcessorId"].Value.ToString();
186 break;
187 } 
188 textBox1.Text +=  "CPU ID:" + strID;
189 
190 //电脑1:CPU ID:BFEBFBFF00000F27
191 
192 //电脑2:CPU ID:BFEBFBFF00000F27
193 
194 //电脑3:CPU ID:BFEBFBFF00000F29
195 
196 //电脑4:CPU ID:BFEBFBFF00000F29
197 
198 //获取主板编号:
199 view plaincopy to clipboardprint?
200 ManagementClass mc = new ManagementClass("Win32_BaseBoard"); 
201 ManagementObjectCollection moc = mc.GetInstances(); 
202 string strID = null ; 
203 foreach( ManagementObject mo in moc ) 
204 { 
205 strID = mo.Properties["SerialNumber"].Value.ToString(); 
206 break; 
207 } 
208 textBox1.Text +=  "主板 ID:" + strID; 
209 ManagementClass mc = new ManagementClass("Win32_BaseBoard");
210 ManagementObjectCollection moc = mc.GetInstances();
211 string strID = null ; 
212 foreach( ManagementObject mo in moc ) 
213 { 
214 strID = mo.Properties["SerialNumber"].Value.ToString();
215 break;
216 } 
217 textBox1.Text +=  "主板 ID:" + strID;
218 
219 //电脑2:主板 ID:CN24401483
220 
221 //电脑3:主板 ID:AZF241001101
222 
223 //获取硬盘编号:
224 view plaincopy to clipboardprint?
225 ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); 
226 //网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。 
227 ManagementObjectCollection moc = mc.GetInstances(); 
228 string strID = null ; 
229 foreach( ManagementObject mo in moc ) 
230 { 
231 strID = mo.Properties["SerialNumber"].Value.ToString(); 
232 break; 
233 } 
234 textBox1.Text +=  "硬盘 ID:" + strID; 
235 ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
236 //网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
237 ManagementObjectCollection moc = mc.GetInstances();
238 string strID = null ; 
239 foreach( ManagementObject mo in moc ) 
240 { 
241 strID = mo.Properties["SerialNumber"].Value.ToString();
242 break;
243 } 
244 textBox1.Text +=  "硬盘 ID:" + strID;
245 
246 //电脑1:硬盘 ID:4833395344463658202020202020202020202020
247 
248  //电脑2:硬盘 ID:WD-WMAJD1092385249 
250 //电脑3:硬盘 ID:4a353756354d5939202020202020202020202020
251 
252 //电脑4:硬盘 ID:0637J2FW508014
253 
254 
255 //获取BIOS编号:
256 view plaincopy to clipboardprint?
257 ManagementClass mc = new ManagementClass("Win32_BIOS"); 
258 ManagementObjectCollection moc = mc.GetInstances(); 
259 string strID = null ; 
260 foreach( ManagementObject mo in moc ) 
261 { 
262 strID = mo.Properties["SerialNumber"].Value.ToString(); 
263 break; 
264 } 
265 textBox1.Text +=  "BIOS ID:" + strID; 
266 ManagementClass mc = new ManagementClass("Win32_BIOS");
267 ManagementObjectCollection moc = mc.GetInstances();
268 string strID = null ; 
269 foreach( ManagementObject mo in moc ) 
270 { 
271 strID = mo.Properties["SerialNumber"].Value.ToString();
272 break;
273 } 
274 textBox1.Text +=  "BIOS ID:" + strID;
275 
276 //电脑2:BIOS ID:CN24401483
277 
278 //由以上各步看出,通过Win32_Processor获取CPUID不正确,或者说Win32_Processor字段就不包含CPU编号信息。
279 
280 //通过Win32_BaseBoard获取主板信息,但不是所有的主板都有编号,或者说不是能获取所有系统主板的编号。
281 
282 //通过Win32_PhysicalMedia获取硬盘编号应该没有问题。但网上说可以通过Win32_DiskDrive获取,其实所得信息根本不包含SerialNumber。
283 
284 //通过Win32_BIOS获取BIOS信息,基本和获取主板信息差不多。就是说:不是所有的主板BIOS信息都有编号。
285 
286 
287 //另外,可以将通过以上各字段所得信息输出,逐个查看所有信息 属性和对应的值。代码如下:
288 
289 view plaincopy to clipboardprint?
290 ManagementClass mc = new ManagementClass("Win32_Processor"); 
291 ManagementObjectCollection moc = mc.GetInstances(); 
292 foreach( ManagementObject mo in moc ) 
293 { 
294 textBox1.Text += "/r/n============CUP信息==========="; 
295 foreach (PropertyData pd in mo.Properties) 
296 { 
297 textBox1.Text += "/r/n" + pd.Name + "/t"; 
298 if (pd.Value != null) 
299 { 
300 textBox1.Text += pd.Value.ToString(); 
301 } 
302 } 
303 textBox1.Text += "/r/n/r/n======================="; 
304 } 

 

posted @ 2016-12-13 16:10  Adolf_Ye  Views(3780)  Comments(0Edit  收藏  举报