C# 读取硬件相关内容信息

在C#编程中,要获取主机名和主机IP地址,是比较容易的.它提供的Dns类,可以轻松的取得主机名和IP地址. 最常见的两个类是:

            一个是Dns类,另一个为IPHostEntry类,二者都存在于命名空间System.Net中. Dns类主要是从域名系统(DNS)中检索关于特定主机的信息,

上面的代码第一行就从本地的DNS中检索出本地主机名. IPHostEntry类则将一个域名系统或主机名与一组IP地址相关联,它与DNS类一起使用,用于获

取主机的IP地址组. 要获取远程主机的IP地址,方法也是相似如下

           1、所使用的命名空间、并在以下方法使用类中导入相关方法:

               using System.Net;

               using System;

               using System.Management;

               using System.Runtime.InteropServices;

              导入:[DllImport("Iphlpapi.dll")]

                      private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);

                      [DllImport("Ws2_32.dll")]

                       private static extern Int32 inet_addr(string ip);

           2、获取本机的IP

              public string getLocalIP()

              {   

                   string mStrPCName = Dns.GetHostName(); //得到本机的主机名   

                   IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP   

                   string mStrPCAddr = ipEntry.AddressList[0].ToString();   

                   return(mStrPCAddr );  
              }

            3、获取本机的MAC

              public string getLocalMac()

             {   

                  string mac = null;   

                 ManagementObjectSearcher query =new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration") ;  

                 ManagementObjectCollection queryCollection = query.Get();   

                 foreach( ManagementObject mo in queryCollection )   

                {      

                      if(mo["IPEnabled"].ToString() == "True")        

                       mac = mo["MacAddress"].ToString();   

                }   

                return(mac);

             }
         4、获取远程主机IP

            public string[] getRemoteIP(string RemoteHostName)

           {   

                 IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);   

                 IPAddress[] IpAddr = ipEntry.AddressList;   

                 string[] strAddr = new string[IpAddr.Length];   

                 for (int i=0;i<IPADDR.LENGTH;I++)   

                {    

                    strAddr[i] = IpAddr[i].ToString();   

                }   

                return(strAddr);

            }

        5、获取远程主机MAC

            public string getRemoteMac(string localIP, string remoteIP)

            {    

                 Int32 ldest= inet_addr(remoteIP); //目的ip   

                 Int32 lhost= inet_addr(localIP); //本地ip
                 try    {    

                              Int64 macinfo = new Int64();    

                              Int32 len = 6;    

                              int res = SendARP(ldest,0, ref macinfo, ref len);    

                              return Convert.ToString(macinfo,16);   

                         }   

                catch(Exception err)   

                {    

                       Console.WriteLine("Error:{0}",err.Message);   

                }   

               return 0.ToString();

            }

       6、简单调用

        getIP gi = new getIP();   

        Console.WriteLine("本地网卡信息:"+gi.getLocalIP() + " - " + gi.getLocalMac());

        Console.WriteLine("\n\r远程网卡信息:");   

        string[] temp = gi.getRemoteIP("tcl-IP");   

        for(int i=0;i<TEMP.LENGTH;I++)   

        {         

              Console.WriteLine(temp[i]);   

        }   

       Console.WriteLine(gi.getRemoteMac("192.168.0.1","192.168.0.2"));

      总结:以上方法是在博客园的博友中无意发现的好东西,感觉很有用。希望对大家有帮助

原文地址:https://www.cnblogs.com/BeyondWJsel/p/2382102.html