Click here to Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhat about alternate DNS server address?
RiccardoF
7:56 12 Dec '08  
First of all congratualtion for the excellent idea! Can the alternate DNS server address be automatically set as well? Plus I noticed that when I apply DHCP settings the DNS is not set to auto.
GeneralThank You
Bruno Tourinho
6:12 24 Sep '08  
Thanks man... you saved my life !!!
GeneralProxy settings
Monir Sabbagh
2:57 25 Mar '08  
Thank you, it is really useful WTF . Can you extend it to include the proxy settings: Adress and port, since you have to change it as well?
GeneralRe: Proxy settings
xExTxCx
6:26 25 Mar '08  
You can change proxy settings like this.


//change IE proxy settings
//
//
private void setIEProxy(String strProxyAddress, int iPort, bool enabled)
{
RegistryKey cUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (enabled == true)
{
cUser.SetValue("ProxyServer", strProxyAddress + ":" + iPort.ToString());
cUser.SetValue("ProxyEnable", 1);
}
else
{
cUser.SetValue("ProxyEnable", 0);
}
}


Brian
GeneralRe: Proxy settings
cri.tt
3:22 10 Jun '08  
Thank you very much for this. I would like to change proxy setting for Mozilla Firefox, can you help me please?
GeneralRe: Proxy settings
xExTxCx
6:31 10 Jun '08  
I know that most of the configuration for FF is stored in .js files, so the configuration will likely have to be done there. Here is a link that might be of some help.

http://forums.mozillazine.org/viewtopic.php?p=3403112&sid=8150b7470870803a0fa2c92eb65ac22c[^]
GeneralVista-UAC
Angelo Cresta
3:57 21 Mar '08  
First of all, thank you for the article.
Now, if the project is loaded in VS2k8 and the OS is Vista, you need to have administrative privileges to enumerate network adapters or to change network settings.
For that reason I'll suggest to add a App.manifest file and set the requestedExecutionLevel to requireAdministrator.

i.e.: <requestedExecutionLevel   level="requireAdministrator" uiAccess="false" />

In that way you'll be asked to run the app as administrator.

Angel
GeneralRe: Vista-UAC
xExTxCx
6:21 21 Mar '08  
Thanks for the heads up. I have been running Vista with UAC disabled, and didn't exhibit the problem. I will look at adding this to the next release.

Brian
GeneralThank!
Tiasys
22:36 20 Mar '08  
Thank!
GeneralOne word
Zoltan Balazs
11:42 20 Mar '08  
Cool!


GeneralRe: One word
xExTxCx
11:50 20 Mar '08  
Thanks!


Last Updated 20 Mar 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010