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

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.