Using C# to Search the Registry
Once you have determined where to find the network information in the Registry of your target system, you can create C# code to search for it and display the information to the user (or use it in your program). In this section, we’ll examine a C# program to query the system Registry for IP information.
The .NET Framework provides a set of classes that help you query and manipulate Registry entries. The .NET Registry classes are contained in two separate namespaces:
Microsoft.Win32.Registry The Microsoft.Win32.Registry namespace contains all the base classes needed to access the base Registry key names: Current_User, Local_Machine, Classes_Root, Current_Config, Dyn_Data, and Users. You must use one of the base Registry key names to build your specific Registry subkey.
Microsoft.Win32.RegistryKey The Microsoft.Win32.RegistryKey namespace contains the classes and methods necessary to query and modify Registry keys and data. The OpenSubKey() method allows you to open a subkey of a known key and access any data values contained in the subkey. Once you have the appropriate subkey, you can use the GetValue() method to get the data values assigned to the subkey.
Listing 2.2 presents the CardGrab.cs program, which will find the network devices installed on a Windows NT, 2000, or XP system using the system Registry, and display the IP information for each device.
Listing 2.2: CardGrab.cs program
using System;
using Microsoft.Win32;
class CardGrab
{
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;
string 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);
serviceName = serviceKey + deviceServiceName +
"\\Parameters\\Tcpip";
networkKey = start.OpenSubKey(serviceName);
if (networkKey == null)
{
Console.WriteLine(" No IP configuration set");
} else
{
string[] ipaddresses =
(string[])networkKey.GetValue("IPAddress");
string[] defaultGateways =
(string[])networkKey.GetValue("DefaultGateway");
string[] subnetmasks =
(string[])networkKey.GetValue("SubnetMask");
foreach(string ipaddress in ipaddresses)
{
Console.WriteLine(" IP Address: {0}",ipaddress);
}
foreach(string subnetmask in subnetmasks)
{
Console.WriteLine(" Subnet Mask: {0}",
subnetmask);
}
foreach(string defaultGateway in defaultGateways)
{
Console.WriteLine(" Gateway: {0}",
defaultGateway);
}
networkKey.Close();
}
}
start.Close();
}
}RegistryKey start = Registry.LocalMachine;
This creates a Registry key object start and sets it to the root class HKLM_LOCAL_MACHINE.
Next, you drill down and set a key object to the pertinent subkey where the network device information is located:
string networkcardKey = "SOFTWARE\\Microsoft\\ Â
Windows NT\\CurrentVersion\\NetworkCards";
RegistryKey serviceNames =
start.OpenSubKey(networkcardKey);Now the key object serviceNames points to the location of the installed network device subkeys.
Next you use the GetSubKeyNames() method to iterate through each of the network device subkey names. Because the same key name is used for the service keys, you can add that value to the known service key location and open the subkey using the OpenSubKey() method, again referenced from the root key:
serviceName = serviceKey + deviceServiceName + "\\Parameters\\Tcpip"; networkKey = start.OpenSubKey(serviceName);
Once each subkey is opened, the IP information is retrieved from the service key using the GetValue() method of the key object. Because each IP information data entry can have multiple values, you must assign the result to a string array and display the complete array using the foreach function:
string[] ipaddresses =
(string[])networkKey.GetValue("IPAddress");
foreach(string ipaddress in ipaddresses)
{
Console.WriteLine(" IP Address: {0}",ipaddress);
}A sample output of the CardGrab program should look like this:
C:\>CardGrab Network card: D-Link DE660 PCMCIA LAN adapter IP Address: 192.168.1.6 Subnet Mask: 255.255.255.0 Gateway: 192.168.1.1 C:\>
You can use a similar technique to find the IP information for Windows 98 or Me devices. In fact, that technique is much simpler because you only have to search in one location for the IP information.

No comments:
Post a Comment