Windows上获得IP地址的四种方法

最近在看网络编程(主要是TCP/IP通信的)的内容,还研究了下WMI。把获得IP地址的方法总结如下。
1.      使用ipconfig程序。
在命令行下使用命令ipconfig可以得到本机的每个有效网罗接口的IP网絡信息。如果在代码中,可以使用Process.Start()方法来调用ipconfig,然后使用正则表达式来解析结果──当然这样做太繁了,不实用。但我曾经使用类似的方法在代码中 获得MAC地址
2.      查找注册表。
使用注册表的麻烦之处在于各个版本的Windows在不同的地方存放网絡信息。
Windows98&Windows Me
注册表中的位置为:
          HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Class\NetTrans
在这下面,每个Key对应一个网络设备的信息(IPAddress,DefaultGateway,IPMask)。
   Windows NT Windows 2000&Windows XP
  和Windows98,Me不同,先要得知有哪些网卡,然后再查此网卡的网絡信息。第一步:找到网卡,地址为:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\NetworkCards,从中得到ServiceName值;
第二步:根据上面得到的ServiceName值到下面地址查找:
HKEY_LOCAL_MACHINE\CurrentControlSet\Services,找到响应的Key后,在parameters\tcpip下可以得到网络设备的信息(IPAddress,DefaultGateway,IPMask)。另外,对于动态的IP地址,可能得去查找DHCPIPAddress注册表對應关键词的值。
3.      使用WMI。
查询表Win32_NetworkAdapterConfiguration即可获得。
4.      使用DNS。
 
代码如下:
None.gif using  System;
None.gif
using  Microsoft.Win32;
None.gif
using  System.Management;
None.gif
using  System.Net;
None.gif
namespace  IPAddress
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class MainClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 應用程式的主進入點。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GetIPByRegistry();
InBlock.gif            GetIPByWMI();
InBlock.gif            GetIPByDns();
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Methods#region Methods
InBlock.gif        
private const string CARDKEY = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards";
InBlock.gif        
private const string SERVICEKEY = @"SYSTEM\CurrentControlSet\Services\";
InBlock.gif        
public static void GetIPByRegistry()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RegistryKey entry 
= Registry.LocalMachine;
InBlock.gif            RegistryKey services 
= entry.OpenSubKey(CARDKEY);
InBlock.gif            
if ( services == null)
InBlock.gif                
return;
InBlock.gif
InBlock.gif            
string[] cards = services.GetSubKeyNames();
InBlock.gif            services.Close();
InBlock.gif
InBlock.gif            
foreach (string key in cards )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RegistryKey cardService 
= entry.OpenSubKey(CARDKEY + "\\" +key);
InBlock.gif                
if ( cardService == null )
InBlock.gif                    
return;
InBlock.gif                
string serviceName = cardService.GetValue("ServiceName").ToString();
InBlock.gif                Console.WriteLine(
"\n Network card : {0}",serviceName);
InBlock.gif                
InBlock.gif                RegistryKey networkCard 
= entry.OpenSubKey(SERVICEKEY+serviceName+"\\Parameters\\Tcpip");
InBlock.gif                
if ( networkCard != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string[] ips = (string[]) networkCard.GetValue("IPAddress");
InBlock.gif                    
foreach ( string ip in ips)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Console.WriteLine(
" The IPAddress is  :{0}",ip);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    networkCard.Close();
ExpandedSubBlockEnd.gif                }

InBlock.gif                entry.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void GetIPByWMI()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string query = "select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled='TRUE'";
InBlock.gif            ManagementObjectSearcher searcher 
= new ManagementObjectSearcher(query);
InBlock.gif            ManagementObjectCollection collection 
= searcher.Get();
InBlock.gif            
foreach ( ManagementObject mo in collection )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] ips = (string[])mo["IPAddress"];
InBlock.gif                
foreach ( string ip in ips )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
" Network card ip is :{0}",ip);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void GetIPByDns()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Net.IPAddress[] ips 
= Dns.GetHostByName(Dns.GetHostName()).AddressList;
InBlock.gif            
foreach ( System.Net.IPAddress ip in ips )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"The ip is : {0}",ip.ToString());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值