码迷,mamicode.com
首页 > Windows程序 > 详细

在Winform获取机器信息IP、MAC、机器名、CPU编号、主硬盘编号

时间:2014-12-18 18:25:24      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   ar   io   color   os   sp   

 

需引入命名空间:

using System.Net;
using System.Management;

 

  1 /// <summary>  
  2 /// 获取本地IP  
  3 /// </summary>  
  4 /// <returns></returns>  
  5 public static string Get_UserIP()  
  6 {  
  7     string ip = "";  
  8     string strHostName = Dns.GetHostName(); //得到本机的主机名  
  9     IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP  
 10     if (ipEntry.AddressList.Length > 0)  
 11     {  
 12         ip = ipEntry.AddressList[0].ToString();  
 13     }  
 14     return ip;  
 15     //string userip = "";  
 16     //string name = Dns.GetHostName();  
 17     //IPAddress[] ips = Dns.GetHostAddresses(name);  
 18     //foreach (IPAddress ip in ips)  
 19     //    userip += ip.ToString() + "|";//所有IP  
 20     //return userip;  
 21 }  
 22 /// <summary>  
 23 /// 获取主机域名  
 24 /// </summary>  
 25 /// <returns></returns>  
 26 public static string Get_HostName()  
 27 {  
 28     return Dns.GetHostName();  
 29 }  
 30 /// <summary>  
 31 /// 获取CPU编号  
 32 /// </summary>  
 33 /// <returns>返回一个字符串类型</returns>  
 34 public static string Get_CPUID()  
 35 {  
 36     try  
 37     {  
 38         //需要在解决方案中引用System.Management.DLL文件  
 39         ManagementClass mc = new ManagementClass("Win32_Processor");  
 40         ManagementObjectCollection moc = mc.GetInstances();  
 41         string strCpuID = null;  
 42         foreach (ManagementObject mo in moc)  
 43         {  
 44             strCpuID = mo.Properties["ProcessorId"].Value.ToString();  
 45             break;  
 46         }  
 47         return strCpuID;  
 48     }  
 49     catch  
 50     {  
 51         return "";  
 52     }  
 53 }  
 54 /// <summary>  
 55 /// 获取第一分区硬盘编号  
 56 /// </summary>  
 57 /// <returns>返回一个字符串类型</returns>  
 58 public static string GetHardDiskID()  
 59 {  
 60     try  
 61     {  
 62         ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");  
 63         string strHardDiskID = null;  
 64         foreach (ManagementObject mo in searcher.Get())  
 65         {  
 66             strHardDiskID = mo["SerialNumber"].ToString().Trim();  
 67             break;  
 68         }  
 69         return strHardDiskID;  
 70     }  
 71     catch  
 72     {  
 73         return "";  
 74     }  
 75 }  
 76 /// <summary>  
 77 /// 获取网卡的MAC地址  
 78 /// </summary>  
 79 /// <returns>返回一个string</returns>  
 80 public static string GetNetCardMAC()  
 81 {  
 82     try  
 83     {  
 84         string stringMAC = "";  
 85         ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");  
 86         ManagementObjectCollection MOC = MC.GetInstances();  
 87         foreach (ManagementObject MO in MOC)  
 88         {  
 89             if ((bool)MO["IPEnabled"] == true)  
 90             {  
 91                 stringMAC += MO["MACAddress"].ToString();  
 92             }  
 93         }  
 94         return stringMAC;  
 95     }  
 96     catch  
 97     {  
 98         return "";  
 99     }  
100 }  
101 /// <summary>  
102 /// 获取当前网卡IP地址  
103 /// </summary>  
104 /// <returns></returns>  
105 public static string GetNetCardIP()  
106 {  
107     try  
108     {  
109         string stringIP = "";  
110         ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");  
111         ManagementObjectCollection MOC = MC.GetInstances();  
112         foreach (ManagementObject MO in MOC)  
113         {  
114             if ((bool)MO["IPEnabled"] == true)  
115             {  
116                 string[] IPAddresses = (string[])MO["IPAddress"];  
117                 if (IPAddresses.Length > 0)  
118                 {  
119                     stringIP = IPAddresses[0].ToString();  
120                 }  
121             }  
122         }  
123         return stringIP;  
124     }  
125     catch  
126     {  
127         return "";  
128     }  
129 }  
130 public static string GetOutIP()  
131 {  
132     string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了   
133     Uri uri = new Uri(strUrl);  
134     System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);  
135     System.IO.Stream s = wr.GetResponse().GetResponseStream();  
136     System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);  
137     string all = sr.ReadToEnd(); //读取网站的数据             
138     int i = all.IndexOf("[") + 1;  
139     string tempip = all.Substring(i, 15);  
140     string ip = tempip.Replace("]", "").Replace(" ", "");//找出i  
141     //也可用  
142     //new Regex(@"ClientIP: \[([\d.]+?)\]").Match(new System.Net.WebClient().DownloadString("http://www.skyiv.com/info/")).Groups[1].Value;  
143     return ip;  
144 }  

 

在Winform获取机器信息IP、MAC、机器名、CPU编号、主硬盘编号

标签:winform   style   blog   http   ar   io   color   os   sp   

原文地址:http://www.cnblogs.com/bo10296/p/4172265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!