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.

6
  • 2
    It is not called the DOS prompt anymore - not since Window spit ME in 1999. Commented Jan 22, 2014 at 22:38
  • 2
    See Edit #3 on this answer for how to escape things stackoverflow.com/a/5635703/231316 Commented Jan 22, 2014 at 22:39
  • OK thanks. I have made changes. But obviously you understand, which is good :) Commented Jan 22, 2014 at 22:40
  • 1
    maybe create a .bat file and call that from your app Commented Jan 22, 2014 at 22:40
  • @ChrisHaas: It worked. Can you please write your comments as a formal answer down here. You deserve the credits. Thank you. Commented Jan 22, 2014 at 22:44

3 Answers 3

18

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hello Nick, what does "/C" mean?
From the referenced SO link: "The /C (capitalized) means "execute whatever else is passed"".
@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.
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"

1 Comment

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"
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();

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.