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 }