jinyuttt

导航

c#获取网关和Ip

方法很多,有简有烦,这里选择了个麻烦的。

主要代码:

  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(); 
        }

posted on 2010-08-22 20:51  代码苦行僧  阅读(716)  评论(0编辑  收藏  举报