Recently, I have been looking at network programming (mainly TCP/IP communication) and WMI. The method for obtaining the IP address is summarized as follows. 1. Use ipconfig Program . You can use the command ipconfig in the command line to obtain the IP network quota information of each valid network interface on the local machine. If Code In, you can use the process. Start () method to call ipconfig, and then use a regular expression to parse the results-of course this is too complicated and not practical. However, I used a similar method to obtain the MAC address in the code. 2. Search for the Registry. The trouble with using the registry is that windows of different versions store the network license information in different places.
Windows 98 & Windows ME In the Registry, the location is HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ class \ nettrans. below, each key corresponds to the information of a network device (IPaddress, DefaultGateway, ipmask ).
Windows NT
,
Windows 2000 & Windows XP Unlike Windows 98 and me, you must first know which NICs are available and then check the network quota information of the NICS. Step 1: Find the NIC at HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ networkcards and obtain the servicename value. Step 2: search for HKEY_LOCAL_MACHINE \ CurrentControlSet \ Services Based on the servicename value obtained above. Find the response key and obtain the network device information (IPaddress, DefaultGateway, ipmask ). In addition, for Dynamic IP addresses, you may need to find the value of the corresponding key word in the dhcpipaddress registry. 3. Use WMI. Query the win32_networkadapterconfiguration table. 4. Use DNS.
The Code is as follows: Using System;
Using Microsoft. Win32;
Using System. Management;
Using System. net;
Namespace IPaddress
{
Class Mainclass
{
/**/ /// <Summary>
///Main entry point of the application.
/// </Summary>
[Stathread]
Static Void Main ( String [] ARGs)
{
Getipbyregistry ();
Getipbywmi ();
Getipbydns ();
Console. Readline ();
}
Methods # Region Methods
Private Const String Cardkey = @" SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ networkcards " ;
Private Const String Servicekey = @" System \ CurrentControlSet \ Services \ " ;
Public Static Void Getipbyregistry ()
{
Registrykey entry = Registry. localmachine;
Registrykey services = Entry. opensubkey (cardkey );
If (Services = Null )
Return ;
String [] Cards = Services. getsubkeynames ();
Services. Close ();
Foreach ( String Key In Cards)
{
Registrykey cardservice = Entry. opensubkey (cardkey + " \\ " + Key );
If (Cardservice = Null )
Return ;
String Servicename = Cardservice. getvalue ( " Servicename " ). Tostring ();
Console. writeline ( " \ N Network Card: {0} " , Servicename );
Registrykey networkcard = Entry. opensubkey (servicekey + Servicename + " \ Parameters \ Tcpip " );
If (Networkcard ! = Null )
{
String [] IPS = ( String []) Networkcard. getvalue ( " IPaddress " );
Foreach ( String IP In IPS)
{
Console. writeline ("The IPaddress is: {0}", Ip );
}
Networkcard. Close ();
}
Entry. Close ();
}
}
Public Static Void Getipbywmi ()
{
String Query = " Select IPaddress from win32_networkadapterconfiguration where ipenabled = 'true' " ;
Managementobjectsearcher searcher = New Managementobjectsearcher (query );
Managementobjectcollection collection = Searcher. Get ();
Foreach (Managementobject Mo In Collection)
{
String [] IPS = ( String []) Mo [ " IPaddress " ];
Foreach ( String IP In IPS)
{
Console. writeline ("Network Card IP is: {0}", Ip );
}
}
}
Public Static Void Getipbydns ()
{
System. net. IPaddress [] IPS = DNS. gethostbyname (DNS. gethostname (). Addresslist;
Foreach (System. net. IPaddress IP In IPS)
{
Console. writeline ("The IP is: {0}", IP. tostring ());
}
}
# Endregion
}
}