1

Thank for reading my thread.

Here is the command line I like to call within my C# code:

C:\>"D:\fiji-win64\Fiji.app\ImageJ-win64.exe" -eval "run('Bio-Formats','open=D:\\Output\\Untitiled032\\ChanA_0001_0001_0001_0001.tif display_ome-xml')"

This is the exact command I can see from my console window, and it runs and gives me what I need. I want to run this command line from from my C# code, so there is escape character problem I don;t know how to handle

There are two strings I'd like to make them flexible

  1. D:\fiji-win64\Fiji.app\ImageJ-win64.exe

  2. D:\Output\Untitiled032\ChanA_0001_0001_0001_0001.tif

I am wondering how I can use string.Format() to formulate this command line?

This is my current code, it opens the image, but the display_ome-xml did not get called:

string bioformats = "Bio-Formats";
string options = string.Format("open={0} display_ome-xml", fileName.Replace("\\", "\\\\"));

string runCommand = string.Format("run(\"'{0}'\",\"'{1}'\")", bioformats, options);
string fijiCmdText = string.Format("/C \"\"{0}\" -eval {1}", fijiExeFile, runCommand);

where fijiExeFile works fins, it is just the runCommand keeps ignoring the display_ome-xml. Anyone has any suggestions? It is really really confusing. Thanks a lot.

5
  • I would try to remove all of those \", they don't seems to be needed neither in the exe name or in the parameters. Also, remove that double replace("\\", "\\\\") Commented May 14, 2014 at 21:41
  • How exactly are you passing the parameters into the C# program? Commented May 14, 2014 at 21:58
  • load the string from xml Commented May 14, 2014 at 21:59
  • Also, where is the code that actually runs the command? If you show that, it will be easier to tell why display_ome-xml is ignored. Commented May 14, 2014 at 22:05
  • 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(); Commented May 14, 2014 at 22:10

2 Answers 2

1

As @Kristian pointed out, @ can help here. It also appears that there may be some extra or misplaced \" in the code above. This seems to give the desired output string:

string fijiExeFile = @"D:\fiji-win64\Fiji.app\ImageJ-win64.exe";
string fileName = @"D:\\Output\\Untitiled032\\ChanA_0001_0001_0001_0001.tif";

string bioformats = "Bio-Formats";
string options = string.Format("open={0} display_ome-xml", fileName);

string runCommand = string.Format("run('{0}','{1}')", bioformats, options);
string fijiCmdText = string.Format("\"{0}\" -eval \"{1}\"", fijiExeFile, runCommand);
Sign up to request clarification or add additional context in comments.

2 Comments

fileName is a string variable, when it is retrieved, it seems it is already in the format of D:\\Output\\Untitled032\\ChanA_0001_0001_0001_0001.tif
OK I have edited my code to eliminate the .Replace since the double `` are already present. You can disregard my first two lines if the fijiExeFile or fileName values are already coming from elsewhere.
1

The easiest way is to use the verbatim string literal. Just put a @ before your string like so:

@"c:\abcd\efgh"

This will disable the backslash escape character

If you need " inside your string, you will have to escape the the quotation marks like so:

@"c:\abcd\efgh.exe ""param1"""

Your example could be:

String.Format(@"""{0}"" -eval ""run('Bio-Formats','open={1} display_ome-xml')""", @"D:\fiji-win64\Fiji.app\ImageJ-win64.exe", @"D:\Output\Untitiled032\ChanA_0001_0001_0001_0001.tif")

or

string p1 = "D:\\fiji-win64\\Fiji.app\\ImageJ-win64.exe";
string p2 = "D:\\Output\\Untitiled032\\ChanA_0001_0001_0001_0001.tif";

String.Format(@"""{0}"" -eval ""run('Bio-Formats','open={1} display_ome-xml')""", p1, p2); 

8 Comments

Thank you for your reply. As you can see, I am really inexperienced at this, so any chance you can extend your answer a little more by writing my command line out using string.Format()? In that way at least I can get some education.
What if the fileName is a string variable, when it is retrieved, it seems it is already in the format of D:\\Output\\Untitled032\\ChanA_0001_0001_0001_0001.tif ?
That's fine, you can just pass it as a parameter to the String.Format
Not working, it does not even start D:\\fiji-win64\\Fiji.app\\ImageJ-win64.exe
Sounds strange, how does the final string look like?
|

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.