4

当 Windows 8 上的路径中有空格时,我在 C# 中启动进程时遇到问题,即使路径是双引号!

以下代码在我们的 XP 和 Windows 7 机器上运行良好多年,但我们最近将一些开发框切换到 Windows 8,现在我们收到以下错误:

'D:\Workspace\Visual' is not recognized as an internal or external command, operable program or batch file.

代码:

string command = "\"D:\\Workspace\\Visual Studio 2010\\Dev\\Tools\\Editors\\AssetManager\\bin\\Tools\\TextureAtlasBuilder.exe\"";
string arguments = "\"D:\\Local\\Temp\\xu2twc4d.cg1\" \"environment-textures\"";

ProcessStartInfo startInfo = new ProcessStartInfo
{
    CreateNoWindow = true,
    UseShellExecute = false,
    WindowStyle = ProcessWindowStyle.Hidden,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    FileName = string.Format(CultureInfo.InvariantCulture, @"{0}\cmd.exe", Environment.SystemDirectory),
    Arguments = string.Format(CultureInfo.InvariantCulture, "/C {0} {1}", command, arguments)
};

Process process = new Process
{
    StartInfo = startInfo
};

process.OutputDataReceived += new DataReceivedEventHandler(StandardOutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(StandardErrorHandler);

process.Start();

我试过带和不带双引号的文字字符串,带和不带双引号的逐字字符串,我总是得到同样的错误!

我究竟做错了什么?!

谢谢

4

2 回答 2

1

从参数字符串中删除 "。

这很棘手,因为 /C 的 cmd 行为对使用 " 非常严格,这可以从 cmd /? 中找到。如果全部失败:将命令行和参数写入临时 cmd 文件并启动该文件...

如果指定了 /C 或 /K,则切换后的命令行的其余部分作为命令行处理,其中使用以下逻辑处理引号 (") 字符:

1.  If all of the following conditions are met, then quote characters
    on the command line are preserved:

    - no /S switch
    - exactly two quote characters
    - no special characters between the two quote characters,
      where special is one of: &<>()@^|
    - there are one or more whitespace characters between the
      two quote characters
    - the string between the two quote characters is the name
      of an executable file.

2.  Otherwise, old behavior is to see if the first character is
    a quote character and if so, strip the leading character and
    remove the last quote character on the command line, preserving
    any text after the last quote character.
于 2012-12-29T14:47:33.760 回答
0

我正在使用以下路径,并且它已成功接受。

string command1 = @"C:\test\ab ab\1.txt";
Process.Start(command1);
于 2012-12-29T14:23:00.713 回答