Nick X Tsui
Nick X Tsui

Reputation: 2912

running dos command line from C#?

I am trying to run this command from command-line prompt:

"D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless"

It works perfect when I type it in a command-line console.

However, when I was trying to make it work from C# application, it failed. I tried following, but seems the command above did not get executed somehow:

string fijiCmdText = "D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText);

Anyone has any idea how to change it to work? Thanks.

Upvotes: 12

Views: 34602

Answers (3)

Nick X Tsui
Nick X Tsui

Reputation: 2912

The problem was solved in the direction Chris Haas pointed out. Simply adding "/C " in front of the path, and it worked for me:

Original code that did not work:

string fijiCmdText = "D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText)

The code that works:

string fijiCmdText = "/C D:\\fiji\\fiji.exe -macro D:\\fiji\\macros\\FFTBatch.ijm --headless";
System.Diagnostics.Process.Start("cmd.exe", fijiCmdText);

Here is the reference mentioned by Chris Haas. See EDIT3

Upvotes: 18

Arin Ghazarian
Arin Ghazarian

Reputation: 5305

You don't have to run cmd.exe, just create ProcessStartInfo object and pass the command with its parameters to it. Like this:

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("your command", "parameters");

Here is an example that shows you how to do it:

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("tree.com", "/f /a");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = info;
p.Start();
p.WaitForExit();

So in your case, this is your command: "D:\\fiji\\fiji.exe" and this is your command parameters or arguments: @"-macro D:\\fiji\\macros\\FFTBatch.ijm --headless"

Upvotes: 11

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Try This:

ProcessStartInfo info = new ProcessStartInfo(@"D:\fiji\fiji.exe",@"-macro D:\fiji\macros\FFTBatch.ijm --headless");
Process process = new Process();
process.StartInfo = info;
process.Start();

Upvotes: 4

Related Questions