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

How to Programmatically Change Network and Computer Settings using VB.NET, netsh, and other Windows Command line programs - Part I - Adding Multiple Addresses using netsh

0.00/5 (No votes)
3 Dec 2013 1  
This article will focus on network settings that can be changed using VB.NET and sometimes relies on command line programs included with Windows.

Introduction

This article will focus on network settings that can be changed using VB.NET and sometimes relies on command line programs included with Windows. Typically, network settings can be changed using the GUI dialogs in Windows but what about using the command line to change the settings in quick session? In addition, some GUI dialogs can be automated as necessary to set settings for Windows internal software or network settings. As this article progresses, I will add more to it and have a part II and maybe some YouTube videos for my fellow programmer enthusiasts. In addition, I will provide a Codeplex page with sample projects for people to download and see the code in live action (will have tested the code myself on Windows 7, maybe Windows XP if I have time). Please refer to the CodePlex page at the end of the article or source code link above.

Note: The below has been tested successfully on Windows XP and Windows 7 but some commands are specific to Windows 7.

First Item on list: Netsh Network Settings Using the Command-line

Netsh is a command-line utility present on the Windows OS's (operating system) to change network settings. It allows the Windows power user or IT user to automate his/her user experience on Windows using scripts or VB.NET. What can be changed with netsh?

Some things that can be programmed with netsh are: security settings on the network, TCP/IP configuration, and some other network related items.

Here's a sample code snippet for adding multiple IP addresses using netsh on Windows 7 with a GUI VB.NET program (code included in the link that follows):

Imports System.IO
Public Class Form1
    Structure SubNetType
        Public Const ClassA = 1
        Public Const ClassB = 2
        Public Const ClassC = 3
    End Structure
    Private Sub Btnaddip_Click(sender As System.Object, _
    	e As System.EventArgs) Handles Btnaddip.Click
        Dim iprangetotal As Integer
        Dim commandprompt As Integer
        Dim SubNet As Integer
        Dim i As Integer
        Dim j As Integer
        Dim b As Integer
        Dim command As String
        commandprompt = Shell("c:\windows\system32\cmd.exe", _
        	AppWinStyle.NormalFocus, True, 1000)
        AppActivate(commandprompt)
        My.Computer.Keyboard.SendKeys("netsh")
        My.Computer.Keyboard.SendKeys("{Enter}")
        My.Computer.Keyboard.SendKeys("interface")
        My.Computer.Keyboard.SendKeys("{Enter}")
        My.Computer.Keyboard.SendKeys("ipv4")
        My.Computer.Keyboard.SendKeys("{Enter}")
        'iprangetotal = 1
        iprangetotal = TxtNumIP.Text
        ' j = 1
        If TxtSubnet.Text.Length <= 9 Then
            If TxtSubnet.Text.Substring(4, 1) = "." Then
                SubNet = SubNetType.ClassA
            End If
        End If
        If TxtSubnet.Text.Length <= 11 Then
            If TxtSubnet.Text.Substring(7, 1) = "." Then
                SubNet = SubNetType.ClassB
            End If
        End If
        ' MsgBox(TxtSubnet.Text.Length)
        If TxtSubnet.Text.Length >= 13 Then
            If TxtSubnet.Text.Substring(13, 1).ToString = "." Then
                SubNet = SubNetType.ClassC
            End If
        End If
        Select Case SubNet
            Case Is = SubNetType.ClassC
                For j = 1 To iprangetotal
                    command = "add address " + Chr(34) + _
                    "Local Area Connection" + Chr(34) + " _
                    addr=" + TxtPartialIP.Text + j.ToString + " _
                    mask=" + TxtSubnet.Text + " gateway=" + _
                    TxtGateway.Text + " gwmetric=5"
                    My.Computer.Keyboard.SendKeys(command)
                    'My.Computer.Keyboard.SendKeys("add address " + _
                    Chr(34) + "Local Area Connection" + Chr(34) + _
                    " addr=192.168.1.3 mask=255.255.255.0 gateway=192.168.1.1 gwmetric=5")
                    My.Computer.Keyboard.SendKeys("{Enter}")
                    System.Threading.Thread.Sleep(2000)
                Next j
            Case Is = SubNetType.ClassB
                For j = 0 To iprangetotal
                    For i = 1 To iprangetotal
                        command = "add address " + Chr(34) + _
                        "Local Area Connection" + Chr(34) + _
                        " addr=" + TxtPartialIP.Text + j.ToString + _
                        "." + i.ToString + " mask=" + _
                        TxtSubnet.Text + " gateway=" + TxtGateway.Text + _
                        " gwmetric=5"
                        My.Computer.Keyboard.SendKeys(command)
                        'My.Computer.Keyboard.SendKeys("add address " + _
                        Chr(34) + "Local Area Connection" + Chr(34) + _
                        " addr=192.168.1.3 mask=255.255.255.0 gateway=192.168.1.1 gwmetric=5")
                        My.Computer.Keyboard.SendKeys("{Enter}")
                        System.Threading.Thread.Sleep(2000)
                    Next i
                Next j
            Case Is = SubNetType.ClassA
                For j = 0 To iprangetotal
                    For b = 0 To iprangetotal
                        For i = 0 To iprangetotal
                            command = "add address " + Chr(34) + _
                            "Local Area Connection" + Chr(34) + _
                            " addr=" + TxtPartialIP.Text + i.ToString + _
                            "." + b.ToString + "." + j.ToString + _
                            " mask=" + TxtSubnet.Text + " gateway=" + _
                            TxtGateway.Text + " gwmetric=5"
                            My.Computer.Keyboard.SendKeys(command)
                            'My.Computer.Keyboard.SendKeys("add address " + _
                            Chr(34) + "Local Area Connection" + Chr(34) + _
                            " addr=192.168.1.3 mask=255.255.255.0 gateway=192.168.1.1 gwmetric=5")
                            My.Computer.Keyboard.SendKeys("{Enter}")
                            System.Threading.Thread.Sleep(2000)
                        Next i
                    Next b
                Next j
        End Select
    End Sub

As you can see, I loop through the subnet entered and depending on the subnet class, add the IP address to it (i.e. class A subnets change after the first octet - 192.0.0.0-192.255.255.255.). I do not start at 0 because that is the network host address instead I start at 1 and go to the number of hosts in the subnet.

Note: For Windows XP version, replace the above IPV4 in code with IP. In addition, more refinements can be done inside and to the for loops to provide better control over the IP addresses entered.

Here's the link to source code:

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