C # obtain the gateway and Ip address

Source: Internet
Author: User

There are a lot of methods, and there is a lot of trouble with simplicity. I chose a lot of trouble here.

Main Code:

Private void getxx (){
RegistryKey start = Registry. LocalMachine;
RegistryKey cardServiceName, networkKey;
String networkcardKey = "SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ NetworkCards ";
String serviceKey = "SYSTEM \ CurrentControlSet \ Services \";
String networkcardKeyName, deviceName;
String deviceServiceName, serviceName;
RegistryKey serviceNames = start. OpenSubKey (networkcardKey );
If (serviceNames = null)
{
MessageBox. Show ("Bad registry key ");
Return;
}
String [] networkCards = serviceNames. GetSubKeyNames ();
ServiceNames. Close ();
Foreach (string keyName in networkCards)
{
NetworkcardKeyName = networkcardKey + "\" + keyName;
CardServiceName = start. OpenSubKey (networkcardKeyName );
If (cardServiceName = null)
{
MessageBox. Show (networkcardKeyName );
Return;
}
DeviceServiceName = (string) cardServiceName. GetValue ("ServiceName ");
DeviceName = (string) cardServiceName. GetValue ("Description ");
MessageBox. Show (deviceName );
ServiceName = serviceKey + deviceServiceName + "\ Parameters \ Tcpip ";
NetworkKey = start. OpenSubKey (serviceName );
If (networkKey = null)
{

......
}
Else
{
String [] ipaddresses = (string []) networkKey. GetValue ("IPAddress ");
String [] defaultGateways = (string []) networkKey. GetValue ("DefaultGateway ");
String [] subnetmasks = (string []) networkKey. GetValue ("SubnetMask ");
Foreach (string ipaddress in ipaddresses)
{
MessageBox. Show (ipaddress );
}
Foreach (string subnetmask in subnetmasks)
{
......}
Foreach (string defaultGateway in defaultGateways)
{
MessageBox. Show (defaultGateway );
}
NetworkKey. Close ();
}
}
Start. Close ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.