1

I wrote a BAT script that automatically connects and disconnects a broad band connection:

netsh mbn connect interface="Mobile Broadband Connection" connmode=name name="My Provider" 
netsh mbn disconnect interface="Mobile Broadband Connection"

When I click the BAT script it is working fine, but when I execute it with Process.Start:

    var startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = "/c reconnect.bat",
        WindowStyle = ProcessWindowStyle.Minimized,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    };

    var process = new Process
    {
        StartInfo = startInfo
    };
    process.Start();

netsh returns an error that the mbn command was not found.

Before I was using a BAT file I started the commands directly. They worked fine on the shell, but got the same error when using Process.Start.

Why is this happening to me?

Output:

C:\Dev\NetworkAdapterTest\NetworkAdapterTest\bin\Debug>netsh mbn connect interface=\"Mobile Breitbandverbindung\" connmode=name name=\"A1 2\" The following command was not found: mbn connect interface="Mobile Breitbandverbindung" connmode=name name="A1 2".

C:\Dev\NetworkAdapterTest\NetworkAdapterTest\bin\Debug>netsh mbn disconnect interface=\"Mobile Breitbandverbindung\" The following command was not found: mbn disconnect interface="Mobile Breitbandverbindung"

Notice how the quoting is really wired. I got the same issues when I started the commands directly.

When I compile the solution with Visual Studio 2008 everything is working as intended.

Question is no longer relevant.

xsl
  • 17,116
  • 18
  • 71
  • 112

2 Answers2

2

The content of your arguments variable doesn't seem to make a lot of sense. If your program is located in "C:\Temp", it will be: "C:\Temp\/c reconnect.bat".
If the bat file is in the same folder as your application, you might want to use this code:

var arguments = string.Format("/c \"{0}\"", 
                  Path.Combine(Application.StartupPath, "reconnect.bat"));

The extra quotes, in case your path has spaces in it.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

Instead of using "cmd.exe", have you tried starting the batch file directly? It should work without having to go through cmd.exe.

The other thing I would check is that you're using the correct path. The easiest way is to have the Bat in the same directory as your executable, or refer to the full path in the filename.

Stack Overflow - how to execute a batch file from windows form

Community
  • 1
  • 1
Mikecito
  • 2,053
  • 11
  • 17
  • I tried to start the bat file directly, with no success - still got the same error. – xsl Apr 07 '11 at 16:23