你的位置:首页 > ASP.net教程

[ASP.net教程]获得服务器硬件信息(CPUID、硬盘号、主板序列号、IP地址等)



1 // 注意:首先要在项目中添加引用 System.Management 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Management; 8 9 public class Computer 10 { 11 public string CpuID; //CPU的ID 12 public int CpuCount; //CPU的个数 13 public string[] CpuMHZ;//CPU频率 单位:hz 14 public string MacAddress;//计算机的MAC地址 15 public string DiskID;//硬盘的ID 16 public string DiskSize;//硬盘大小 单位:bytes 17 public string IpAddress;//计算机的IP地址 18 public string LoginUserName;//操作系统登录用户名 19 public string ComputerName;//计算机名 20 public string SystemType;//系统类型 21 public string TotalPhysicalMemory; //总共的内存 单位:M 22 public string BoardID;// 主板序列号 23 public string BoardType;// 主板型号 24 private static Computer _instance; 25 public static Computer Instance() 26 { 27 if (_instance == null) 28 _instance = new Computer(); 29 return _instance; 30 } 31 public Computer() 32 { 33 CpuID = GetCpuID(); 34 CpuCount = GetCpuCount(); 35 CpuMHZ = GetCpuMHZ(); 36 MacAddress = GetMacAddress(); 37 DiskID = GetDiskID(); 38 DiskSize = GetSizeOfDisk(); 39 IpAddress = GetIPAddress(); 40 LoginUserName = GetUserName(); 41 SystemType = GetSystemType(); 42 TotalPhysicalMemory = GetTotalPhysicalMemory(); 43 ComputerName = GetComputerName(); 44 BoardID = GetBoardID(); 45 BoardType = GetBoardType(); 46 } 47 string GetCpuID() 48 { 49 try 50 { 51 //获取CPU序列号代码 52 string cpuInfo = " ";//cpu序列号 53 ManagementClass mc = new ManagementClass("Win32_Processor"); 54 ManagementObjectCollection moc = mc.GetInstances(); 55 foreach (ManagementObject mo in moc) 56 { 57 cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); 58 } 59 moc = null; 60 mc = null; 61 return cpuInfo; 62 } 63 catch 64 { 65 return "unknow "; 66 } 67 finally 68 { 69 } 70 71 } 72 public static int GetCpuCount() 73 { 74 try 75 { 76 using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) 77 { 78 ManagementObjectCollection cpus = mCpu.GetInstances(); 79 return cpus.Count; 80 } 81 } 82 catch 83 { 84 } 85 return -1; 86 } 87 public static string[] GetCpuMHZ() 88 { 89 ManagementClass mc = new ManagementClass("Win32_Processor"); 90 ManagementObjectCollection cpus = mc.GetInstances(); 91 92 string[] mHz = new string[cpus.Count]; 93 int c = 0; 94 ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); 95 foreach (ManagementObject mo in mySearch.Get()) 96 { 97 mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); 98 c++; 99 }100 mc.Dispose();101 mySearch.Dispose();102 return mHz;103 }104 public static string GetSizeOfDisk()105 {106 ManagementClass mc = new ManagementClass("Win32_DiskDrive");107 ManagementObjectCollection moj = mc.GetInstances();108 foreach (ManagementObject m in moj)109 {110 return m.Properties["Size"].Value.ToString();111 }112 return "-1";113 }114 string GetMacAddress()115 {116 try117 {118 //获取网卡硬件地址119 string mac = " ";120 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");121 ManagementObjectCollection moc = mc.GetInstances();122 foreach (ManagementObject mo in moc)123 {124 if ((bool)mo["IPEnabled"] == true)125 {126 mac = mo["MacAddress"].ToString();127 break;128 }129 }130 moc = null;131 mc = null;132 return mac;133 }134 catch135 {136 return "unknow ";137 }138 finally139 {140 }141 142 }143 string GetIPAddress()144 {145 try146 {147 //获取IP地址 148 string st = " ";149 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");150 ManagementObjectCollection moc = mc.GetInstances();151 foreach (ManagementObject mo in moc)152 {153 if ((bool)mo["IPEnabled"] == true)154 {155 //st=mo[ "IpAddress "].ToString(); 156 System.Array ar;157 ar = (System.Array)(mo.Properties["IpAddress"].Value);158 st = ar.GetValue(0).ToString();159 break;160 }161 }162 moc = null;163 mc = null;164 return st;165 }166 catch167 {168 return "unknow ";169 }170 finally171 {172 }173 174 }175 string GetDiskID()176 {177 try178 {179 //获取硬盘ID 180 String HDid = " ";181 ManagementClass mc = new ManagementClass("Win32_DiskDrive");182 ManagementObjectCollection moc = mc.GetInstances();183 foreach (ManagementObject mo in moc)184 {185 HDid = (string)mo.Properties["Model"].Value;186 }187 moc = null;188 mc = null;189 return HDid;190 }191 catch192 {193 return "unknow ";194 }195 finally196 {197 }198 199 }200 /// <summary> 201 /// 操作系统的登录用户名 202 /// </summary> 203 /// <returns> </returns> 204 string GetUserName()205 {206 try207 {208 string st = " ";209 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");210 ManagementObjectCollection moc = mc.GetInstances();211 foreach (ManagementObject mo in moc)212 {213 214 st = mo["UserName"].ToString();215 216 }217 moc = null;218 mc = null;219 return st;220 }221 catch222 {223 return "unknow ";224 }225 finally226 {227 }228 229 }230 string GetSystemType()231 {232 try233 {234 string st = " ";235 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");236 ManagementObjectCollection moc = mc.GetInstances();237 foreach (ManagementObject mo in moc)238 {239 240 st = mo["SystemType"].ToString();241 242 }243 moc = null;244 mc = null;245 return st;246 }247 catch248 {249 return "unknow ";250 }251 finally252 {253 }254 255 }256 string GetTotalPhysicalMemory()257 {258 try259 {260 261 string st = " ";262 ManagementClass mc = new ManagementClass("Win32_ComputerSystem");263 ManagementObjectCollection moc = mc.GetInstances();264 foreach (ManagementObject mo in moc)265 {266 267 st = mo["TotalPhysicalMemory"].ToString();268 269 }270 moc = null;271 mc = null;272 return st;273 }274 catch275 {276 return "unknow ";277 }278 finally279 {280 }281 }282 string GetComputerName()283 {284 try285 {286 return System.Environment.GetEnvironmentVariable("ComputerName");287 }288 catch289 {290 return "unknow ";291 }292 finally293 {294 }295 }296 297 string GetBoardID()298 {299 string strbNumber = string.Empty;300 try301 {302 ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard");303 foreach (ManagementObject mo in mos.Get())304 {305 strbNumber = mo["SerialNumber"].ToString().Trim('.');306 break;307 }308 }309 catch (Exception ex)310 {311 strbNumber = "unknow";312 }313 314 return strbNumber;315 }316 317 /// <summary>318 /// 主板型号319 /// </summary>320 /// <returns></returns>321 string GetBoardType()322 {323 string st = "";324 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");325 foreach (ManagementObject mo in mos.Get())326 {327 st = mo["Product"].ToString();328 }329 return st;330 }331 332 333 }