0

I used to have a small tool i created in VB.net to enable/disable my Ethernet.

Right now i am trying to recreate it in C# but i cannot seem to figure out how to get the command to work.

The following gives a error, probably because i am clueless with C#.

private void btnDisabled_Click(object sender, EventArgs e)
{
    Process.Start("CMD", "netsh interface set interface "Ethernet" DISABLED");
}

Which is supposed to enter netsh interface set interface "Ethernet" DISABLED in command prompt.

I clearly have the entire code wrong, but i cant find out how it should be.

Anybody got any advice?

Thanks

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
Sevy
  • 13
  • 3
  • 1
    First of all, you should escape the `"` around `Ethernet` => `\"Ethernet\"` then give us the error :) – Thomas Ayoub Aug 27 '15 at 14:25
  • If i add the escape it gives no error. But it just opens command prompt but does not send the command. – Sevy Aug 27 '15 at 15:44
  • ProcessStartInfo info = new ProcessStartInfo("cmd.exe"); info.Arguments = "/K netsh interface set interface /Ethernet DISABLED"; Process.Start(info); Tried that, but same result does not enter the command. – Sevy Aug 27 '15 at 15:50

3 Answers3

2

You can test this one.

ENABLE

static void Enable(string interfaceName)
{
 System.Diagnostics.ProcessStartInfo psi =
        new System.Diagnostics.ProcessStartInfo("netsh", String.Format("interface set interface {0} enable", interfaceName ));
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo = psi;
    p.Start();
}

DISABLE

static void Disable(string interfaceName)
{
    System.Diagnostics.ProcessStartInfo psi =
        new System.Diagnostics.ProcessStartInfo("netsh", String.Format("interface set interface {0} disable", interfaceName ));
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo = psi;
    p.Start();
}

In one Method.

    static void SetInterface(string interfaceName, bool enable)
    {
        string type;
        if (enable == true) type = "enable";
        else type = "disable";

        System.Diagnostics.ProcessStartInfo psi =
            new System.Diagnostics.ProcessStartInfo("netsh", String.Format("interface set interface {0} {1}", interfaceName, type));
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo = psi;
        p.Start();
    }
  • While correct, this answer shows a lot of code duplication. It would be better to provide a single method with a boolean parameter indicating whether to start or stop. – Eric J. Aug 27 '15 at 14:26
0

I think the following answer should help Why does Process.Start("cmd.exe", process); not work?. You may also need to execute the process with admin rights (see How to start a Process as administrator mode in C#)

Community
  • 1
  • 1
Paul Houlston
  • 246
  • 2
  • 7
0

You didn't include the error it's outputting, but from reading the above, it looks like you're putting "Ethernet" is escaping your string and it's attempting to access the Ethernet object rather sending to the command line. If you want to pass a quote mark in a string, you can put \" instead of ".

Graham
  • 336
  • 1
  • 13