字符串格式在C#中运行命令行 [英] string format in c# running command line
问题描述
如果我运行下面的控制台命令,它会成功:
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\
其中
C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app(1)\fiji.exe
是应用程序文件,
C:\Users\myAccount.Unit\Favorites\Downloads\fiji.app (1)\macros\FFTBatch.ijm
是被执行,
和 C中的宏文件:\Users\myAccount.Unit\Documents\Untitled001\
是以前的宏处理的图像
and C:\Users\myAccount.Unit\Documents\Untitled001\
is the images processed by the previous macro.
不过,。当我使用C#来做好这项工作,它失败(即没有响应)。以下是相关代码:
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;
}
(路径实际上是从UI获得的,我只是。发布它是什么)
这是我所观察到 fijiCmdText
当我一步:
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\\"
如果我只用
string fijiCmdText = string.Format("/C \"{0}\"", _fijiExeFile);
它确实启动的.exe程序,但如果我打算添加的宏文件的路径,它失败。
It does launch the .exe application, however if I intended to add the macro file path, it fails.
错在这里的任何东西?
推荐答案
链接我张贴的状态你需要使用引号/ S开关和把整个命令:
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);
或者,更清楚:
string fijiCmdText = "/S /C \"<command line that can have quotes>\"";
这篇关于字符串格式在C#中运行命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!