c#(asp.net)获取机器信息(IP)(MAC)(机器名)(CPU编号)(主硬盘编号)

  • Post author:
  • Post category:其他


第一种: 获取计算机Ip地址




View Code

 1       string user_IP;
2 if (Request.ServerVariables["HTTP_VIA"] != null)
3 {
4 user_IP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
5 }
6 else
7 {
8 user_IP = Request.ServerVariables["REMOTE_ADDR"].ToString();
9 }
10
11 Response.Write(user_IP);


1.获取机器名:




View Code

1 public string GetHostName()
2 {
3 return System.Net.Dns.GetHostName();
4 }

2.获取CPU编号




View Code

 1 public string GetCpuId()
2
3 {
4
5 ManagementClass mc = new ManagementClass("Win32_Processor");
6 ManagementObjectCollection moc = mc.GetInstances();
7
8 String strCpuID = null ;
9 foreach( ManagementObject mo in moc )
10 {
11 strCpuID = mo.Properties["ProcessorId"].Value.ToString();
12 break;
13 }
14 return strCpuID;
15
16 }

3.获取主硬盘编号




View Code

 1 public string GetMainHardDiskId()
2
3 {
4
5 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
6 String strHardDiskID = null ;
7 foreach(ManagementObject mo in searcher.Get())
8 {
9 strHardDiskID = mo["SerialNumber"].ToString().Trim();
10 break;
11 }
12 return strHardDiskID ;
13
14 }


//获取硬盘ID




View Code

1 String HDid;
2 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
3 ManagementObjectCollection moc1 = cimobject1.GetInstances();
4 foreach(ManagementObject mo in moc1)
5 {
6 HDid = (string)mo.Properties["Model"].Value;
7 Response.Write ("硬盘序列号:"+HDid.ToString ());
8 }

//获取网卡硬件地址




View Code

1 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
2 ManagementObjectCollection moc2 = mc.GetInstances();
3 foreach(ManagementObject mo in moc2)
4 {
5 if((bool)mo["IPEnabled"] == true)
6 Response.Write("MAC addresst{0}"+mo["MacAddress"].ToString());
7 mo.Dispose();
8 }
9 }

转载于:https://www.cnblogs.com/wwjsq/archive/2011/11/20/2255689.html