命令行参数中的空格不起作用
本文关键字:空格 不起作用 参数 命令行 | 更新日期: 2023-09-27 18:15:43
我需要执行命令行参数。如果文件路径包含空格,则无法正常工作。它返回未找到的错误文件。程序如下:
public void Method()
{
string docFile = @"C:'Test Document1.doc";
string docxFile = @"C:'Test Document1.docx";
string file = @"C:'doc2x_r649 (1)'doc2x_r649'doc2x.exe";
ExecuteCommand(file, string.Format(docFile + " -o " + docxFile));
}
public static string ExecuteCommand(string file, string command)
{
String result;
try
{
//Create a new ProcessStartInfo
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
//Settings
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.FileName = file;
procStartInfo.Arguments = command;
//Create new Process
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//Set ProcessStartInfo
proc.StartInfo = procStartInfo;
//Start Process
proc.Start();
//Wait to exit
proc.WaitForExit();
//Get Result
result = proc.StandardOutput.ReadToEnd();
//Return
return result;
}
catch
{
}
return null;
}
如果文件路径不包含空格,则正常工作
您是否尝试过在路径中添加引号?
ExecuteCommand(file, string.Format("'"" + docFile + "'" -o '"" + docxFile + "'""));
试试这个
ExecuteCommand(file, string.Format("'"{0}'" -o '"{1}'"",docFile , docxFile));