在路径中包含参数和空格时,使用Process.Start。

46

我看过类似的例子,但找不到完全符合我的问题的解决方案。

我需要从C#运行这样一个命令:

C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe p1=hardCodedv1 p2=v2

我在运行时设置了v2,所以在调用Process.Start之前需要能够在C#中修改字符串。 有人知道如何处理这个问题吗?因为我的参数之间有空格。


ProcessStartInfo: http://msdn.microsoft.com/zh-cn/library/system.diagnostics.processstartinfo.aspx - wgraham
2
传递给ProcessStartInfo.Filename或Process.Start(string, string)的路径中的空格并不是问题。只有解析字符串的程序可能会因此混淆,例如cmd.exe。 - Hans Passant
@HansPassant,vlc.exe 也被文件名中的空格所困扰。因此我必须使用 Steve 的建议来让 Procees.Start 为我工作。 - LetzerWille
6个回答

51

即使您使用ProcessStartInfo类,如果必须为参数添加空格,则上面的答案将无法解决问题。有一个简单的解决方案。只需在参数周围添加引号即可。

 string fileName = @"D:\Company Accounts\Auditing Sep-2014 Reports.xlsx";
 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
 startInfo.FileName = "Excel.exe";
 startInfo.Arguments = "\"" + fileName + "\"";
 System.Diagnostics.Process.Start(startInfo);

我在文件名周围添加了转义引号,这样就可以正常工作了。


2
这也可以用一行代码完成,使用 Process.Start("Excel.exe", """ + fileName + """); - Red_Shadow
10
是的,但我现在想强调添加引号来避免初学者混淆。 - Abdul Saleem

23
您可以使用ProcessStartInfo类来分隔您的参数、文件名、工作目录和参数,而不必担心空格。
string fullPath = @"C:\FOLDER\folder with spaces\OTHER_FOLDER\executable.exe"
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Path.GetFileName(fullPath);
psi.WorkingDirectory = Path.GetDirectoryName(fullPath);
psi.Arguments = "p1=hardCodedv1 p2=" + MakeParameter();
Process.Start(psi);

MakeParameter是一个函数,返回用于参数p2的字符串。


10

尝试这个

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName =  "\"C:\\FOLDER\\folder with   spaces\\OTHER_FOLDER\\executable.exe\"";
startInfo.Arguments = "p1=hardCodedv1 p2=v2";
Process.Start(startInfo);

1
这很完美,只是没有startInfo.Start()方法。我不得不像@Steve建议的那样使用Process.Start(startInfo)。谢谢你的帮助。 - WEFX

6

在查看其他提供的解决方案后,我发现我的各种参数都被捆绑成了一个参数。

例如: "-setting0=arg0 --subsetting0=arg1"

因此,我建议采用以下方法:

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "\"" + Prefs.CaptureLocation.FullName + "\"";
        psi.Arguments = String.Format("-setting0={0} --subsetting0={1}", "\"" + arg0 + "\"", "\"" + arg1+ "\"");
        Process.Start(psi);

将每个参数用引号包围,而不是整个参数集合。正如Red_Shadow所指出的那样,这可以通过单行代码完成。

        Process.Start("\"" + filename + "\"", arguments here)

0
非常微妙的细节: 如果我使用ArgumentList,它只在严格修剪(没有前导或尾随空格)并且每个字符串都在自己的数组位置时才起作用。
    var psi = new ProcessStartInfo("cmd") {
        ArgumentList = {
            "--somepath",
            "/root/subpath",
            "--someid",
            "12345",
        }
    };

如果我尝试举例

 "--somepath /root/subpath",

它不起作用,我从 cmd 应用程序中得到错误信息。

甚至

        "--somepath ",

会破坏接收程序的解析器。


0

如果您想要启动的进程是cmd /C,
并且参数包含多个空格,例如
C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /N /T C:\users\someuser\AppData\Local\Temp\temp file with spaces.tmp Printer Name with Spaces
也许这个答案可以帮到您:

https://dev59.com/rGw15IYBdhLWcg3w0fGh#6378038

简而言之:双引号。
 string arguments ="/C \"\"C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe\" /N /T \"C:\\users\\someuser\\AppData\\Local\\Temp\\temp file with spaces.tmp\" \"Printer Name with Spaces\"\"";
 ProcessStartInfo procStartInfo = new ProcessStartInfo("C:\\Windows\\sysnative\\cmd.exe",arguments);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接