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

3
  • 1
    First of all, you should escape the " around Ethernet => \"Ethernet\" then give us the error :) Commented Aug 27, 2015 at 14:25
  • If i add the escape it gives no error. But it just opens command prompt but does not send the command. Commented Aug 27, 2015 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. Commented Aug 27, 2015 at 15:50

3 Answers 3

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();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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#)

Comments

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 ".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.