zoukankan      html  css  js  c++  java
  • 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"));

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

  • 相关阅读:
    寒假要学习的知识点
    Linux firewall防火墙 换成 iptables 防火墙
    git add出现 "fatal: in unpopulated submodule XXX" 错误
    使用navicat连接mysql报10038错时解决方法
    php 获取url重定向
    php 获取文件后缀的几种方法
    Windows+PHP7.3环境下安装imagick扩展和imagemagick
    php7 连接数据库的方法
    Warning from https://mirrors.aliyun.com/composer: Support for Composer 1 is deprecated and some packages will not be available. You should upgrade to Composer 2.
    php 执行GuzzleHttp请求时发生cURL error 60: SSL certificate problem错误的解决方法
  • 原文地址:https://www.cnblogs.com/BeyondWJsel/p/2382102.html
Copyright © 2011-2022 走看看