

Introduction
I created NetProfiles because there was no good way to manage multiple IP profiles within Windows. As a network engineer, I travel to multiple client sites - each having unique IP settings. NetProfiles allows users to setup multiple profiles to automate this process.
Background
In this article, I will demonstrate several techniques:
- Using C# to manipulate the registry.
- Enumerating a list of network adapters and getting settings for each.
- Programmatically changing IP addresses, DNS servers, and static routes.
Using the code
NetProfiles relies on the .NET Framework version 2.0.
I have included the compiled binary (x86) and the Visual Studio project (VS2008).
- Manipulating the registry:
RegistryKey cUser = Registry.LocalMachine;
cUser = cUser.OpenSubKey("SOFTWARE",true);
cUser.CreateSubKey("AntiDesign\\NetProfiles\\__Adapters");
cUser = cUser.OpenSubKey("AntiDesign\\NetProfiles\\__Adapters",true);
for (int i = 0; i < cboSelectAdapter.Items.Count; i++)
{
cUser.CreateSubKey(cboSelectAdapter.Items[i].ToString());
RegistryKey tmp = cUser.OpenSubKey(cboSelectAdapter.Items[i].ToString(), true);
tmp.SetValue("CurrentProfile", "");
tmp.SetValue("RouteNetwork", "");
}
- Enumerating network adapters:
private void loadAdapters()
{
foreach (NetworkInterface netInterface in
NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet |
netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
cboSelectAdapter.Items.Add(netInterface.Name);
}
}
try
{
cboSelectAdapter.SelectedIndex = 0;
}
catch { }
}
- Changing IP, DNS servers, and routing:
string args, argsDNS;
args = "interface ip set address name=\"" + AdapterName + "\" static " +
ipAddress + " " + subnetMask + " " + gateway + " 1";
argsDNS = "interface ip set dns name=\"" + AdapterName + "\" static " +
dnsServer;
Process netsh = new Process();
netsh.StartInfo.FileName = "netsh.exe";
netsh.StartInfo.UseShellExecute = false;
netsh.StartInfo.CreateNoWindow = true;
netsh.StartInfo.Arguments = args;
netsh.Start();
netsh.WaitForExit();
netsh.StartInfo.Arguments = argsDNS;
netsh.Start();
netsh.WaitForExit();
netsh.Dispose();
Process cmd = new Process();
cmd.StartInfo.FileName = "route.exe";
cmd.StartInfo.Arguments = "add " + routeNetwork + " mask " +
routeMask + " " + routeGateway + " metric " + routeMetric;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.WaitForExit();
cmd.Dispose();
Points of interest
This app is relatively lightweight, and provides useful functionality that is not provided by Windows.
History
- 3-20-2008 - Uploaded content.