12

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.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73

3 Answers3

16

The problem was solved as in the direction Chris Haas pointed out. It does not mean other answers don't work, it just means the problem can be solved at least in one way.

Here it is, simply adding "/C " in the code, and it should work:

Original that did not work:

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

;

Current 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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • 2
    Hello Nick, what does "/C" mean? – Vu Nguyen Sep 04 '14 at 10:03
  • From the referenced SO link: "The /C (capitalized) means "execute whatever else is passed"". – Russell Fox Feb 17 '17 at 18:38
  • 1
    @VuNguyen You can in a dos command prompt window just type: cmd /? to find out that /C means: ''Carries out the command specified by string and then terminates". Usually, dos commands and parameters are not case-specific. – Roland Apr 01 '20 at 08:14
11

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"

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
  • You only have to run CMD for command line commands like COPY that have no EXE and are actually built-in commands of cmd.exe: `new ProcessStartInfo("CMD", "/C COPY A.TXT B.TXT"` – Roland Apr 02 '20 at 14:42
4

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();
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67