5,666,132 members and growing! (16,487 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » Utilities     Intermediate License: The Code Project Open License (CPOL)

NetProfiles - Multiple IP Profiles Using C# and .NET

By xExTxCx

A utility that allows users to store multiple IP profiles that can be changed on the fly.
C# (C# 1.0, C# 2.0, C# 3.0, C#), .NET (.NET, .NET 2.0), Win32, Visual Studio (Visual Studio, VS2008), CEO, Architect, DBA, Dev, QA, Design

Posted: 20 Mar 2008
Updated: 20 Mar 2008
Views: 9,978
Bookmarked: 31 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.50 Rating: 4.47 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 11.8%
3
2 votes, 11.8%
4
13 votes, 76.5%
5

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

xExTxCx



Occupation: Systems Engineer
Company: ETC
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralThank YoumemberBruno Tourinho6:12 24 Sep '08  
GeneralProxy settingsmemberMonir Sabbagh2:57 25 Mar '08  
GeneralRe: Proxy settingsmemberxExTxCx6:26 25 Mar '08  
GeneralRe: Proxy settingsmembercri.tt3:22 10 Jun '08  
GeneralRe: Proxy settingsmemberxExTxCx6:31 10 Jun '08  
GeneralVista-UACmemberAngelo Cresta3:57 21 Mar '08  
GeneralRe: Vista-UACmemberxExTxCx6:21 21 Mar '08  
GeneralThank!memberTiasys22:36 20 Mar '08  
GeneralOne wordmemberZoltan Balazs11:42 20 Mar '08  
GeneralRe: One wordmemberxExTxCx11:50 20 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Mar 2008
Editor: Smitha Vijayan
Copyright 2008 by xExTxCx
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project