CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  VB.NET

如何获取计算机的网卡名和相应的IP地址?

楼主zjh135(aaa)2004-08-04 15:52:43 在 .NET技术 / VB.NET 提问

如何获取计算机的网卡名和相应的IP地址?即用IPCONFIG获到的信息相同 问题点数:0、回复次数:4Top

1 楼mingfei200169(木目)回复于 2004-08-05 23:01:50 得分 0

网卡ID   号在标准的.NET命名空间中是无法获得的,必须用P/Invoke调用一个win32API方法来找到它,获得相应的IP地址  
   
  Imports   System  
  Imports   System.Net  
  Imports   System.Net.Sockets  
  Imports   System.Text.RegularExpressions  
  Imports   Microsoft.VisualBasic  
   
          Module   M_TestIPAddress  
                  Class   TestIPAddress  
   
              '通过dns主机名获得IP地址信息  
                          Private   Shared   Sub   IPAddresses(ByVal   server   As   String)  
                                  Try  
                                          Dim   ASCII   As   New   System.Text.ASCIIEncoding  
   
                                          '   Get   server   related   information.  
                                          Dim   heserver   As   IPHostEntry   =   Dns.Resolve(server)  
   
                                          '   Loop   on   the   AddressList  
                                          Dim   curAdd   As   IPAddress  
                                          For   Each   curAdd   In   heserver.AddressList  
   
                                                  '   Display   the   type   of   address   family   supported   by   the   server.   If   the  
                                                  '   server   is   IPv6-enabled   this   value   is:   InternNetworkV6.   If   the   server  
                                                  '   is   also   IPv4-enabled   there   will   be   an   additional   value   of   InterNetwork.  
                                                  Console.WriteLine(("AddressFamily:   "   +   curAdd.AddressFamily.ToString()))  
   
                                                  '   Display   the   ScopeId   property   in   case   of   IPV6   addresses.  
                                                  If   curAdd.AddressFamily.ToString()   =   ProtocolFamily.InterNetworkV6.ToString()   Then  
                                                          Console.WriteLine(("Scope   Id:   "   +   curAdd.ScopeId.ToString()))  
                                                  End   If  
                                                  '显示标准格式的ip地址  
                                                  Console.WriteLine(("Address:   "   +   curAdd.ToString()))  
   
                                          Console.WriteLine(ControlChars.CrLf)  
                                          Next   curAdd  
   
                                  Catch   e   As   Exception  
                                          Console.WriteLine(("[DoResolve]   Exception:   "   +   e.ToString()))  
                                  End   Try  
                          End   Sub   'IPAddresse  
                                           
   
                          Public   Shared   Sub   Main(ByVal   args()   As   String)  
                                  Dim   server   As   String   =   Nothing  
   
                                  '   Define   a   regular   expression   to   parse   user's   input.  
                                  '   This   is   a   security   check.   It   allows   only  
                                  '   alphanumeric   input   string   between   2   to   40   character   long.  
                                  'Define   a   regular   expression   to   parse   user's   input.  
                                  'This   is   a   security   check.   It   allows   only  
                                  'alphanumeric   input   string   between   2   to   40   character   long.  
                                  Dim   rex   As   New   Regex("^[a-zA-Z]\w{1,39}$")  
   
                                  If   args.Length   <   1   Then  
                                          '   If   no   server   name   is   passed   as   an   argument   to   this   program,   use   the   current    
                                          '   server   name   as   default.  
                                          server   =   Dns.GetHostName()       '获得DNS主机名  
                                          Console.WriteLine(("Using   current   host:   "   +   server))  
                                  Else  
                                          server   =   args(0)  
                                          If   Not   rex.Match(server).Success   Then  
                                                  Console.WriteLine("Input   string   format   not   allowed.")  
                                                  Return  
                                          End   If  
                                  End   If  
   
                                  '   Get   the   list   of   the   addresses   associated   with   the   requested   server.  
                                  IPAddresses(server)          
   
                                  '   Get   additonal   address   information.  
                                  IPAddressAdditionalInfo()  
                                  Console.Read()  
                          End   Sub   'Main  
                  End   Class   'TestIPAddress  
          End   Module  
   
   
   
   
   
   
   
  Top

2 楼wahahasnail(笑东笑西笑南笑北笑来笑去笑自己原来无知无识)回复于 2004-08-06 10:00:33 得分 0

用WMI处理  
  Top

3 楼koomis()回复于 2005-03-26 16:08:43 得分 0

Dim   mc   As   System.Management.ManagementClass   =   New   System.Management.ManagementClass("Win32_NetworkAdapterConfiguration")  
  Dim   moc   As   System.Management.ManagementObjectCollection   =   mc.GetInstances()  
  Dim   mo   As   System.Management.ManagementObject  
  For   Each   mo   In   moc  
        If   CBool(mo("IPEnabled"))   =   True   Then  
                        MessageBox.Show(mo("MacAddress").ToString())   '//---网卡MAC地址  
                        MessageBox.Show(mo("IPAddress")(0).ToString())   '//---网卡IP地址  
        End   If  
  Next  
  Top

4 楼daisi(腾飞天涯)回复于 2005-03-26 17:05:36 得分 0

楼上正解Top

相关问题

  • 求教:写出一个通用程序,可以获取某台计算机上的本地IP地址和网卡(MAC)地址
  • 如何获取网络上计算机的名称和IP?
  • 怎样通过IP地址,获取计算机的名称?
  • 获取计算机名和IP出错,大虾帮忙
  • 静态页面(htm)如果获取ip和计算机名?
  • 请教如何获取计算机名和IP地址。。急
  • 如何取得计算机中两张网卡的IP地址?(在线等待)
  • 网卡ip本机能ping通,其他局域网内计算机ping不通
  • 如何获取当前计算机的IP地址和主机名?
  • 如何获取计算机的名称,描述,IP地址和MAC地址?

关键词

  • .net
  • ipv6
  • ip
  • server
  • 网卡
  • curadd
  • 相应的ip地址
  • 获得
  • dim

得分解答快速导航

  • 帖主:zjh135

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo