I am trying to change a computer's IP address from inside a VB project using netsh. I can get the cmd prompt to open but it doesn't pass the arguments. Any thoughts? Thanks

Private Sub BtnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTest.Click
        Dim IPmsg As String
        Dim NetshArgs As String
        ' Dim LAC As String = "Local Area Connection"

        NetshArgs = "netsh interface ip set address name=Local Area Connection static (IPmsg)"

        IPmsg = Microsoft.VisualBasic.InputBox("Enter IP information", "Enter IP subnet and gateway with a sigle space between each.", "", 500, 700)

        Process.Start("cmd.exe", NetshArgs)
    End Sub

Dani AI

Generated

Two things tend to trip folks up here: elevation and targeting the right adapter reliably. Building on ’s suggestion, you can avoid cmd.exe entirely and call netsh directly, elevated, which makes argument passing simpler. Also consider using the interface index instead of the adapter’s display name so you are not fighting spaces or name changes. You can discover the index with netsh interface ipv4 show interfaces, then pass name=<index> in the command. (learn.microsoft.com)

Here is a minimal VB.NET pattern that prompts for elevation and invokes netsh directly. Replace the placeholders with your values (or parse them from your input UI). Note the use of the numeric interface index.

Imports System.Diagnostics

Dim ip As String = "192.168.1.50"
Dim mask As String = "255.255.255.0"
Dim gw As String = "192.168.1.1"
Dim ifaceIndex As String = "15"  ' from: netsh interface ipv4 show interfaces

Dim psi As New ProcessStartInfo()
psi.FileName = "netsh"
psi.Arguments = "interface ipv4 set address name=" & ifaceIndex &
                " static " & ip & " " & mask & " " & gw
psi.Verb = "runas"                 ' triggers UAC prompt
psi.UseShellExecute = True
psi.CreateNoWindow = True
psi.WindowStyle = ProcessWindowStyle.Hidden

Using p = Process.Start(psi)
    p.WaitForExit()
End Using

Practical tips:

  • Run your VB app as admin or keep Verb="runas" so the netsh call succeeds; many netsh commands require elevation. Microsoft also notes that, on modern Windows, PowerShell networking cmdlets are preferred to netsh; if you ever move off netsh, New-NetIPAddress and friends are the supported path. (learn.microsoft.com)
  • If you want to avoid spawning netsh entirely, you can set IPs via WMI from VB by calling Win32_NetworkAdapterConfiguration.EnableStatic, SetGateways, and SetDNSServerSearchOrder on the target adapter’s configuration object. Those methods map directly to IP, mask, gateway, and DNS without shelling out. (learn.microsoft.com)

Recommended Answers

All 2 Replies

Something like this might work:

    Private Sub BtnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTest.Click
        Dim IPmsg As String
        Dim NetshArgs As String
        IPmsg = Microsoft.VisualBasic.InputBox("Enter IP information", "Enter IP subnet and gateway with a sigle space between each.", "", 500, 700)
        Dim NewProcess As New  ProcessStartInfo

        'Change the '/k' to a '/c' if you don't want cmd to remain open 
        NewProcess.Arguments = "/k netsh interface ip set address name=Local Area Connection static " & IPmsg
        NewProcess.FileName = "cmd"
        Process.Start(NewProcess)
    End Sub         

I had to add double quotes arounf local area connection which I add forgotten in the first post, other than that it works great.

     Private Sub BtnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTest.Click
    Dim IPmsg As String
    Dim NetshArgs As String
    IPmsg = Microsoft.VisualBasic.InputBox("Enter IP information", "Enter IP subnet and gateway with a sigle space between each.", "", 500, 700)
    Dim NewProcess As New ProcessStartInfo
    'Change the '/k' to a '/c' if you don't want cmd to remain open
    NewProcess.Arguments = "/k netsh interface ip set address name=""Local Area Connection"" static " & IPmsg
    NewProcess.FileName = "cmd"
    Process.Start(NewProcess)
    End Sub 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.