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

Recommended Answers

All 3 Replies

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);
            }
        }
    }
}

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?

Thanks.
Solved it with process start.

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

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.