0

Hello people,
i have made this .bat few years ago and its working fine for changing gateway

getaway 192.168.1.1

@echo off
netsh interface ip set dns name="Local Area Connection" source=static addr=192.168.1.1
netsh interface ip set address name="Local Area Connection" source=static addr=192.168.1.11 mask=255.255.255.0 gateway=192.168.1.1 gwmetric=0
exit

getaway 192.168.1.5

@echo off
netsh interface ip set dns name="Local Area Connection" source=static addr=192.168.1.5
netsh interface ip set address name="Local Area Connection" source=static addr=192.168.1.11 mask=255.255.255.0 gateway=192.168.1.5 gwmetric=0
exit

My question is:
How to port this to C# code?

Thanks in advance
Happy new year all

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by nesa24casa
0

I think you'll probably need the Process class. The following is taken from the MSDN article, about that. With this class you could run the .bat or with a little more digging you should be able to run netsh directly.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
0

You could do this using the WMI. However, your batch file would be the easiest way to do things. Why do you want to change it?

0

Thanks.
Solved it with process start.

I need change - one network modem with it .1 is for browser
second .5 is for for gaming

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.