【问题标题】:Spaces in file path passed as command line argument文件路径中的空格作为命令行参数传递
【发布时间】:2015-02-20 07:24:39
【问题描述】:

我正在尝试将文件路径作为命令行参数传递。如果没有空格的路径可以正常工作,但如果有空格则不行。在下面的代码中,commandText“scriptPath”甚至可以使用空格。 但是变量“file1”和“file2”不能使用空格。

string scriptFilePath = "@" + string.Format("\"{0}\"", "D:\\Script\\ScriptFile.txt");
string file1 = @"D:\New Folder\file1.png";
string file2 = @"D:\New Folder\file2.png";
string outPutPath = @"D:\New Folder\Output\Report.html";

string commandText = "/c " + "BCompare.exe" + scriptFilePath + " " + file1 + " " + file2 + " " + outPutPath;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = @exePath;
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.Arguments = commandText;
proc = Process.Start(startInfo);
proc.WaitForExit();

【问题讨论】:

  • 格式化你的问题,这是不可读的!
  • 嗨,Nathan Tuggy,这里的要求是我必须在 BCompare.exe 中运行一个脚本文件(比较两个图像文件)。因此,我通过命令提示符在 BCompare.exe 中运行脚本。为了更好地理解,我在上面编辑了我的代码。我要在命令提示符下执行的命令是 BCompare.exe @D:\Script\ScriptFile.txt D:\New Folder\file1.PNG D:\New Folder\file2.PNG D:\New Folder\Output\Report。 html。这里,file1 & file2 是传递给 scriptFile.txt 的参数,它将在指定的路径中生成 Report.html。
  • 我很想将此标记为 stackoverflow.com/questions/15061854/… 的副本。

标签: c#


【解决方案1】:

将文件路径放在引号中。

【讨论】:

    【解决方案2】:

    请先修复您的代码:

    • 缺少分号
    • 修复代码中的路径并使其可读(使用 String.Format 并使用 @)
    • 我认为您想使用命令行执行“executable.exe”,因此使用“executable.exe”作为 startInfo.FileName,并将剩余的参数(scriptPath、file1、file2)添加到 startInfo.Arguments 并制作确保它们都在双引号之间

    【讨论】:

      【解决方案3】:

      解决方案 1

      string file1 = @"D:\New Folder\file1.png";
      

      解决方案 2

      string file2 = "\"D:\\New Folder\\file2.png\"";
      

      【讨论】:

      • 您好,谢谢您的回复。文件名已经用引号括起来。我尝试用@符号为文件路径添加前缀。这不起作用。解决方案 1 和 2:尝试过,不起作用
      • 抱歉,我来自不允许(或我无法)格式化代码的 android 应用程序。我更正了解决方案 2:试试吧!
      猜你喜欢
      • 2012-09-24
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 2019-02-15
      • 2011-11-24
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多