Reputation: 1357
Thanks a lot for reading my thread. I have a command line which I'd like to format as a string in C#. Here is how the string looks in command line prompt:
I think I only need to know the format starting from run.......
, I guess I can handle fiji -eval
. So this is what I am not su to format:
In case you cannot see it clearly, I re-type the string here:
run("'Bio-Formats Importer'", "'open=[D:\\fiji\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'")
The thing confuses me is the '' inside the "", which I am not confident. Anyone has any idea how to format this command line? Thanks a lot!
A little more edit to extend it to dynamic:
string fileName = string.Empty;
string[] filesList = System.IO.Directory.GetFiles(folder, "*.tif");
fileName = filesList[0];
string bioformats = "Bio-Formats Importer";
options = string.Format("open=[{0}] display_ome-xml", fileName);
runCommand = string.Format("run(\"'{0}'\",\"'{1}'\")", bioformats, options);
string fijiCmdText = string.Format("/C \"\"{0}\" -eval {1}", fijiExeFile, runCommand);
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)
{
ex.ToString();
ret = 0;
}
Upvotes: 1
Views: 222
Reputation: 149068
You could use a normal string literal, but you'd have to escape the the quotes with \"
and backslashes with \\
, like this:
var str = "run(\"'Bio-Formats Importer'\",\"'open=[D:\\\\fiji\\\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'\")";
Or by using a verbatim string literal, but you need to escape the quotes with ""
, like this:
var str = @"run(""'Bio-Formats Importer'"",""'open=[D:\\fiji\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'"")";
Further Reading
Regarding your update, it seems like the problem is that you need to double the slashes in the file path for the benefit of the command line string. I would also recommend simplifying it to something like this:
using System.IO;
using System.Diagnostics;
var filesList = Directory.GetFiles(folder, "*.tif");
var bioformats = "Bio-Formats Importer";
foreach(var fileName in filesList) // loop through every file
{
var options = string.Format("open=[{0}] display_ome-xml", fileName.Replace("\\", "\\\\"));
var args = string.Format("-eval run(\"'{0}'\",\"'{1}'\")", bioformats, options);
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = fijiExeFile,
Arguments = args,
}
};
process.Start();
process.WaitForExit();
ret = 1;
}
catch (Exception ex)
{
ex.ToString();
ret = 0;
}
}
Upvotes: 3
Reputation:
To escape, use \
. For example, "run(\"'Bio-Formats Importer'\""
. For the \
characters, use \\
, or in your case \\\\
.
You can also use string.Format()
to parameterize this: string.Format("run(\"'{0}'\", ...", arg0);
Upvotes: 1