How can I call a script (*.cmd) file from within a .NET application?

  • Thread starter Thread starter Peter Beyersdorf
  • Start date Start date
P

Peter Beyersdorf

Hello,

I want to call a shell script (*.cmd) file from a .NET application.
Does anybody have a hint for me, which and method I can use?

Thank you!

Peter
 
Peter Beyersdorf said:
I want to call a shell script (*.cmd) file from a .NET application.
Does anybody have a hint for me, which and method I can use?

If you don't mind showing the ugly console window then this is one way:

class MyTest
{
[STAThread]
static void Main(string[] args)
{
ProcessStartInfo info;

info = new ProcessStartInfo("cmd.exe", "/K dir /s");
info.WorkingDirectory = "C:\\";
System.Diagnostics.Process.Start(info);
}
}

Regards,
Will
 
If you don't mind showing the ugly console window then this is one way: [...]

Will,

your code works perfectly. It allows me even to keep the console
window open after the script finished execution. This way I can see if
there were some error messages.

Thank you!

Peter
 
Peter Beyersdorf said:
your code works perfectly.

I try not to post snippets that don't work. :-)
It allows me even to keep the console
window open after the script finished execution. This way I can see if
there were some error messages.

Just by the way, that is a function of the "/K" switch. You'll get different
behavior without the switch. If you are not familair with the switches for
the command line interpreter then you can type

cmd /?

in a console window to review the complete list of them.
Thank you!

You are welcome.

Regards,
Will
 
You can set this up so that the window is hidden but you can also
redirect stdout and stderr so that you can get them. This is probably
more useful, since you can do something programmatically if there's an
error.
I want to call a shell script (*.cmd) file from a .NET application.
Does anybody have a hint for me, which and method I can use?


If you don't mind showing the ugly console window then this is one way:

class MyTest
{
[STAThread]
static void Main(string[] args)
{
ProcessStartInfo info;

info = new ProcessStartInfo("cmd.exe", "/K dir /s");
info.WorkingDirectory = "C:\\";
System.Diagnostics.Process.Start(info);
}
}

Regards,
Will
 
Back
Top