-1

If I run following command in console, it will succeed:

"C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\fiji.exe" -macro "C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\macros\FFTBatch.ijm" C:\Users\myAccount.Unit\Documents\Untitled001\

where "C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\fiji.exe" is the application file,

"C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\macros\FFTBatch.ijm" is the macro file that gets executed,

and C:\Users\myAccount.Unit\Documents\Untitled001\ is the images processed by the previous macro.

However, when I use C# to do this job, it failed (meaning no response at all). Following is the relevant code:

string _fijiExeFile = "C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\fiji.exe";
string _ijmFile = "C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\macros\\FFTBatch.ijm";
string _inputDir = "C:\\Users\\myAccount.Unit\\Documents\\Untitled001\\";
string fijiCmdText = string.Format("/C \"{0}\" -macro \"{1}\" {2}", _fijiExeFile, _ijmFile, _inputDir);

try
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new        System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = fijiCmdText;
    process.StartInfo = startInfo;
    process.Start();
    _processOn = true;
    process.WaitForExit();

    ret = 1;
}
catch (Exception ex)
{
    ret = 0;
}

(The path are actually obtained from the UI, I am just posting what it is.)

This is what I observed for fijiCmdText when I step in:

"/C \"C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\fiji.exe\" -macro \"C:\\Users\\myAccount.Unit\\Favorites\\Downloads\\fiji.app (1)\\macros\\FFTBatch.ijm\" C:\\Users\\myAccount.Unit\\Documents\\Untitled001\\"

If I only use

string fijiCmdText = string.Format("/C \"{0}\"", _fijiExeFile);

It does launch the .exe application, however if I intended to add the macro file path, it fails.

Any thing wrong here?

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • 1
    Yes. Don't ever ignore exceptions. At least display `ex.ToString()` so that you can know what's going wrong. – John Saunders Feb 04 '14 at 22:09
  • @JohnSaunders It does not go to exceptions. – Nick X Tsui Feb 04 '14 at 22:11
  • If I step in and it did not hit the catch? – Nick X Tsui Feb 04 '14 at 22:15
  • I would get the value of `fijiCmdText` then try to run from an actual command line. – 001 Feb 04 '14 at 22:21
  • I pasted the actual value of fijiCmdText, I cannot see anything wrong? – Nick X Tsui Feb 04 '14 at 22:23
  • Not sure if this is the issue, but open a command prompt, type "cmd.exe /?" and read the part on quotes. – 001 Feb 04 '14 at 22:28
  • It works when there is no macro path involved. basically, fijiCmdText = string.Format("/C \"{0}\"", _fijiExeFile); works – Nick X Tsui Feb 04 '14 at 22:31
  • Here's your answer: http://stackoverflow.com/questions/355988/how-do-i-deal-with-quote-characters-when-using-cmd-exe – 001 Feb 04 '14 at 22:34
  • @JohnnyMopp I am sorry I am not an English speaker, so I am not sure if I extract information from your link correctly. How do I change the command line as the thread pointed out? like /S /C? It did not work. – Nick X Tsui Feb 04 '14 at 22:49
  • It also does not make sense why it worked when there is no macro path, because if you noticed the .exe file path contains space. – Nick X Tsui Feb 04 '14 at 22:55
  • @NickXTsui , you should make it work directly through command line first, then if working you can worry about C# – meda Feb 04 '14 at 23:18
  • It does work in the command line. It is just the formatting issue which is exactly what I am asking here. – Nick X Tsui Feb 04 '14 at 23:20

1 Answers1

1

The link I posted states that you need to use the /S switch and put the entire command in quotes:

string fijiCmdText = string.Format("/S /C \"\"{0}\" -macro \"{1}\" {2}\"", _fijiExeFile, _ijmFile, _inputDir);

Or, more clearly:

string fijiCmdText = "/S /C \"<command line that can have quotes>\"";
001
  • 13,291
  • 5
  • 35
  • 66
  • Yes worked. It is the \" in front of the \"{0}\" that solves the whole problem. Care to explain a little why? or a link to explain this? Thanks a lot!!! – Nick X Tsui Feb 05 '14 at 20:19
  • I think that means deliminate the whole command line instead of processing them individually. – Nick X Tsui Feb 05 '14 at 20:21