Csharp/CSharp Tutorial/Windows/Registry Network — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

Find DNS Servers and get server list

using System;
using Microsoft.Win32;
class MainClass
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
   }
}

Get default gateway from network key stored in Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main()
    {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey = "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName, deviceServiceName, serviceName;
    
      RegistryKey serviceNames = start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
          Console.WriteLine("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)
          {
            Console.WriteLine("Bad registry key: {0}", networkcardKeyName);
            return;
          }
          deviceServiceName = (string)cardServiceName.GetValue("ServiceName");
          deviceName = (string)cardServiceName.GetValue("Description");
          serviceName = serviceKey + deviceServiceName + "\\Parameters\\Tcpip";
          networkKey = start.OpenSubKey(serviceName);
          if (networkKey == null)
          {
            Console.WriteLine("    No IP configuration set");
            return;
          }
          string[] defaultGateways = (string[])networkKey.GetValue("DefaultGateway");
          foreach(string defaultGateway in defaultGateways)
          {
              Console.WriteLine("    Gateway: {0}", defaultGateway);
          }
          networkKey.Close();
      }
      start.Close();
    }
}

Get DNS server from Registry

using System;
using Microsoft.Win32;
class MainClass
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
   }
}

Get IP address from network key stored in Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main()
    {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey = "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName, deviceServiceName, serviceName;
    
      RegistryKey serviceNames = start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
          Console.WriteLine("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)
          {
            Console.WriteLine("Bad registry key: {0}", networkcardKeyName);
            return;
          }
          deviceServiceName = (string)cardServiceName.GetValue("ServiceName");
          deviceName = (string)cardServiceName.GetValue("Description");
          serviceName = serviceKey + deviceServiceName + "\\Parameters\\Tcpip";
          networkKey = start.OpenSubKey(serviceName);
          if (networkKey == null)
          {
            Console.WriteLine("    No IP configuration set");
            return;
          }
          string[] ipaddresses = (string[])networkKey.GetValue("IPAddress");
        
          foreach(string ipaddress in ipaddresses)
          {
              Console.WriteLine("    IP Address: {0}", ipaddress);
          }
          networkKey.Close();
      }
      start.Close();
    }
}
IP Address: 0.0.0.0
    IP Address: 0.0.0.0

Get Network Card Information from Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main()
    {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey = "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName, deviceServiceName, serviceName;
    
      RegistryKey serviceNames = start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
          Console.WriteLine("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)
          {
            Console.WriteLine("Bad registry key: {0}", networkcardKeyName);
            return;
          }
          deviceServiceName = (string)cardServiceName.GetValue("ServiceName");
          deviceName = (string)cardServiceName.GetValue("Description");
          Console.WriteLine("\nNetwork card: {0}", deviceName);
      }
    
      start.Close();
    }
}
Network card: Broadcom 802.11b/g WLAN
Network card: Realtek RTL8139/810x Family Fast Ethernet NIC

Get subnet mask from network key stored in Registry

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main()
    {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey = "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName, deviceServiceName, serviceName;
    
      RegistryKey serviceNames = start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
          Console.WriteLine("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)
          {
            Console.WriteLine("Bad registry key: {0}", networkcardKeyName);
            return;
          }
          deviceServiceName = (string)cardServiceName.GetValue("ServiceName");
          deviceName = (string)cardServiceName.GetValue("Description");
          serviceName = serviceKey + deviceServiceName + "\\Parameters\\Tcpip";
          networkKey = start.OpenSubKey(serviceName);
          if (networkKey == null)
          {
            Console.WriteLine("    No IP configuration set");
            return;
          }
          string[] subnetmasks = (string[])networkKey.GetValue("SubnetMask");
          foreach(string subnetmask in subnetmasks)
          {
              Console.WriteLine("    Subnet Mask: {0}", subnetmask);
          }
          networkKey.Close();
      }
      start.Close();
    }
}
Subnet Mask: 0.0.0.0
    Subnet Mask: 0.0.0.0

Get the network device name, device service name

using System;
using Microsoft.Win32;
class MainClass
{
    public static void Main()
    {
      RegistryKey start = Registry.LocalMachine;
      RegistryKey cardServiceName, networkKey;
      string networkcardKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
      string serviceKey = "SYSTEM\\CurrentControlSet\\Services\\";
      string networkcardKeyName, deviceName, deviceServiceName, serviceName;
    
      RegistryKey serviceNames = start.OpenSubKey(networkcardKey);
      if (serviceNames == null)
      {
          Console.WriteLine("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)
          {
            Console.WriteLine("Bad registry key: {0}", networkcardKeyName);
            return;
          }
          Console.WriteLine(networkcardKeyName);
          deviceServiceName = (string)cardServiceName.GetValue("ServiceName");
          Console.WriteLine(deviceServiceName);
          deviceName = (string)cardServiceName.GetValue("Description");
          Console.WriteLine("Network card: {0}", deviceName);
      }
      start.Close();
    }
}
SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\11
{4FA13198-E7E5-4065-8395-EDC576EE1020}
Network card: Broadcom 802.11b/g WLAN
SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\2
{0180EFAF-14BF-42AA-A274-6D8D766A0093}
Network card: Realtek RTL8139/810x Family Fast Ethernet NIC

Parse server list stored in Registry

using System;
using Microsoft.Win32;
class MainClass
{
   public static void Main()
   {
      RegistryKey start = Registry.LocalMachine;
      string DNSservers = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters";
      RegistryKey DNSserverKey = start.OpenSubKey(DNSservers);
      if (DNSserverKey == null)
      {
         Console.WriteLine("Unable to open DNS servers key");
         return;
      }
  
      string serverlist = (string)DNSserverKey.GetValue("NameServer");
      Console.WriteLine("DNS Servers: {0}", serverlist);
      DNSserverKey.Close();
      start.Close();
      char[] token = new char[1];
      token[0] = " ";
      string[] servers = serverlist.Split(token);
      foreach(string server in servers)
      {
         Console.WriteLine("DNS server: {0}", server);
      }
   }
}