zoukankan      html  css  js  c++  java
  • 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(); 
            }

  • 相关阅读:
    微软的十年之变
    如何在易受攻击的SSD上禁用硬件BitLocker加密
    Tech Summit 2018见闻:我们,MVP
    Tech Summit 2018见闻:IT之家读者捕捉铺路集团董事长玄隐
    Windows 10怎么了?
    循环队列
    模拟键盘事件
    模拟鼠标事件
    进程间通信——— 匿名管道
    进程间通信——— LPC
  • 原文地址:https://www.cnblogs.com/jinyuttt/p/1805933.html
Copyright © 2011-2022 走看看