Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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); 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top