Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NetProfiles - Multiple IP Profiles Using C# and .NET

0.00/5 (No votes)
20 Mar 2008 1  
A utility that allows users to store multiple IP profiles that can be changed on the fly.

npMain.JPG

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:

  1. Using C# to manipulate the registry.
  2. Enumerating a list of network adapters and getting settings for each.
  3. 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).

  1. Manipulating the registry:
    //Connect to the registry and create the Keys necessary for 
    //NetProfiles
    //
    RegistryKey cUser = Registry.LocalMachine;
    cUser = cUser.OpenSubKey("SOFTWARE",true);
    cUser.CreateSubKey("AntiDesign\\NetProfiles\\__Adapters");
    cUser = cUser.OpenSubKey("AntiDesign\\NetProfiles\\__Adapters",true);
    
    //create keys for all network adapters
    //
    //
    for (int i = 0; i < cboSelectAdapter.Items.Count; i++)
    {
        //create subkeys for each network adapter found
        cUser.CreateSubKey(cboSelectAdapter.Items[i].ToString());
        RegistryKey tmp = cUser.OpenSubKey(cboSelectAdapter.Items[i].ToString(), true);
            
        //set default values inside the created keys
        tmp.SetValue("CurrentProfile", "");
            tmp.SetValue("RouteNetwork", "");
    }
  2. Enumerating network adapters:
    //Function to populate combo box with a list of network adapters
    //
    //
    private void loadAdapters()
    {
        foreach (NetworkInterface netInterface in 
                 NetworkInterface.GetAllNetworkInterfaces())
        {
            // Only get ethernet and wireless adapters
            if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet | 
                netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
            {
                cboSelectAdapter.Items.Add(netInterface.Name);//add to list
            }
        }
        try
        {
            cboSelectAdapter.SelectedIndex = 0;
        }
        catch { }
    }
  3. Changing IP, DNS servers, and routing:
    //build the argument strings for netsh.exe
    //
    //
    string args, argsDNS;
    
    args = "interface ip set address name=\"" + AdapterName + "\" static " +
        ipAddress + " " + subnetMask + " " + gateway + " 1";
    argsDNS = "interface ip set dns name=\"" + AdapterName + "\" static " +
        dnsServer;
                        
    //apply the changes using netsh
    //
    //
    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();
    
    //Add the dns server
    //
    //
    netsh.StartInfo.Arguments = argsDNS;
    netsh.Start();
    netsh.WaitForExit();
    netsh.Dispose();
    
    
    //add the static route
    //
    //
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here