程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#獲取網關和Ip

c#獲取網關和Ip

編輯:C#入門知識

方法很多,有簡有煩,這裡選擇了個麻煩的。

主要代碼:

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

      

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved