【问题标题】:Whitespaces at the Process Arguments进程参数中的空格
【发布时间】:2016-11-20 18:46:02
【问题描述】:

我正在尝试使用 Visual Studio 2015 VSIX 项目创建 Visual Studio 加载项。加载项应该能够运行 .exe 文件以及当前项目路径。目前我可以找到项目的路径并将其作为进程的参数(.exe 文件)。 .exe文件通过cmd运行如下:

    the.exe -d projectDirectoryPath

检测项目路径不是我的问题,但是,如果路径包含 Space Character ,则将其理解为单独的参数。 以下代码是我用来启动该过程的代码:

string myDir = activeProjectPath();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = pathOfExe + "\\the.exe";
startInfo.Arguments = "-d " + myDir;

当当前项目的路径不包含空格字符时,代码可以正常工作,否则无法启动进程。该过程给出以下错误:

给定参数:-d C:\Users\theUser\Documents\Visual Studio 2015\Projects\testProject\testProject

您的命令行中指定的参数过多!跳过额外的参数:Studio

您的命令行中指定的参数过多!跳过额外的参数:2015\Projects\testProject\testProject

注意:当我尝试从 cmd 运行 .exe 时,它​​接受项目路径中的空格字符。

我该如何解决这个问题?

谢谢!

【问题讨论】:

  • 将路径放在引号中
  • windows 对空格的处理能力很差。坦率地说,他们不应该让它,因为它决定 c:\program files\something\my.exe 是 c:\program - 哦不存在并且 files\也不存在的东西\my.exe ..引号是必不可少的

标签: c# windows visual-studio command-line-arguments whitespace


【解决方案1】:

尽量使用 " 字符。您可以按以下形式指定路径: 例如,“C:\Users\foo\My Documents\file name.sln”。 您可以使用 " 字符包围任何路径,例如在批处理或 Powershell 脚本中。

startInfo.Arguments = string.Concat("-d", "\"", myDir, "\"");

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2010-10-22
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多