通过 Process.Start 传递带有 space 作为可执行文件参数的路径

Pass path with space as parameter of executable via Process.Start

我有带反斜杠和 space 的路径,我需要将其作为参数发送到 regedit.exe:

\folder1\folder2\folder three\file.reg

据我所知,在字符串前面使用 @ 应该允许直接指定反斜杠(无需转义)。这是我试图在其上执行的完整代码:

string path = @"\folder1\folder2\folder three\file.reg"
Process regeditProcess = Process.Start("regedit.exe", file);

当我尝试 运行 这个程序时,它从 regedit 的输出中给我一个错误:

Cannot import \folder1\folder2\folder: Error opening the file. There may be a disk or file system error

由于错误报告反斜杠正确,我猜测编译器或 regedit 在 "folder"

之后没有读取白色 space 之后的任何内容

在命令行传递参数时,需要用"括起来。试试这个:

string path = @"""\folder1\folder2\folder three\file.reg""";
Process regeditProcess = Process.Start("regedit.exe", path);

在逐字字符串中添加 "" 会在字符串中添加一个双引号,因此生成的字符串将为 "\folder1\folder2\folder three\file.reg",然后可以将其正确传递给 regedit.exe .