非常感谢您阅读我的帖子。我有一个命令行,我想在 C# 中将其格式化为字符串。以下是字符串在命令行提示符中的样子:
我想我只需要知道从run.......开始的格式,我想我可以处理 fiji -eval 。 所以这是我不习惯格式化的:
如果你看不清楚,我在这里重新输入字符串:
run("'Bio-Formats Importer'", "'open=[D:\\fiji\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'")
让我感到困惑的是“”中的'',我对此没有信心。任何人都知道如何格式化此命令行?非常感谢!
再做一点修改以将其扩展为动态:
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;
}
请您参考如下方法:
您可以使用普通的字符串文字,但您必须使用 \" 转义引号,使用 \\ 转义反斜杠,如下所示:
var str = "run(\"'Bio-Formats Importer'\",\"'open=[D:\\\\fiji\\\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'\")";
或者使用逐字字符串文字,但您需要使用 "" 转义引号,如下所示:
var str = @"run(""'Bio-Formats Importer'"",""'open=[D:\\fiji\\ChanA_0001_0001_0001_0001.tif] display_ome-xml'"")";
延伸阅读
关于您的更新,问题似乎在于您需要将文件路径中的斜杠加倍,以便使用命令行字符串。我还建议将其简化为如下所示:
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;
}
}
