简体   繁体   English

使用 Process.Start 参数和路径中的空格

[英]Use Process.Start with parameters AND spaces in path

I've seen similar examples, but can't find something exactly like my problem.我见过类似的例子,但找不到与我的问题完全相同的东西。

I need to run a command like this from C#:我需要从 C# 运行这样的命令:

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

I'm setting v2 at runtime, so I need to be able to modify the string in C# before calling Process.Start.我在运行时设置 v2,所以我需要能够在调用 Process.Start 之前修改 C# 中的字符串。 Does anyone know how to handle this, since I have spaces between my parameters?有谁知道如何处理这个,因为我的参数之间有空格?

Even when you use the ProcessStartInfo Class, if you have to add spaces for arguments, then the above answers won't solve the problem. 即使您使用ProcessStartInfo类,如果您必须为参数添加空格,那么上述答案将无法解决问题。 There's a simple solution. 有一个简单的解决方案。 Just add quotes around arguments. 只需在参数周围添加引号即可。 That's all. 就这样。

 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);

Here I've added escaped quotes around filename, and it works. 在这里,我添加了围绕文件名的转义引号,它的工作原理。

You can use the ProcessStartInfo class to separate your arguments, FileName, WorkingDirectory and arguments without worry for spaces 您可以使用ProcessStartInfo类来分隔您的参数,FileName,WorkingDirectory和参数,而无需担心空格

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);

where MakeParameter is a function that returns the string to be used for the p2 parameter 其中MakeParameter是一个函数,它返回用于p2参数的字符串

Try this 试试这个

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);

After looking at the other solutions provided I ran into the issue where all my various arguments were bundled into one argument. 在查看提供的其他解决方案之后,我遇到了所有我的各种参数被捆绑到一个参数中的问题。

ie "-setting0=arg0 --subsetting0=arg1" "-setting0=arg0 --subsetting0=arg1"

So I would propose the following: 所以我建议如下:

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

With the quotes around each argument, instead of around the entire set of arguments. 使用每个参数周围的引号,而不是围绕整个参数集。 And as pointed out by Red_Shadow this can all be done with the single line 正如Red_Shadow所指出的,这一切都可以通过单行来完成

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

Very subtle caveat:非常微妙的警告:
If I use ArgumentList , it only works when using it strictly trimmed (without leading or trailing spaces) and each string in it's own array place.如果我使用ArgumentList ,它只有在使用它时才有效(没有前导或尾随空格)并且每个字符串都在它自己的数组位置。

    var psi = new ProcessStartInfo("cmd") {
        ArgumentList = {
            "--somepath",
            "/root/subpath",
            "--someid",
            "12345",
        }
    };

If I try for example例如,如果我尝试

 "--somepath /root/subpath",

it doesn't work and I get errors from the cmd application.它不起作用,我从cmd应用程序中收到错误。

Even甚至

        "--somepath ",

breaks the parser of the receiving program.破坏接收程序的解析器。

If the process you want to start is cmd /C如果你要启动的进程是cmd /C
and the argument contains multiple spaces like并且参数包含多个空格,例如
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 C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /N /T C:\users\someuser\AppData\Local\Temp\temp file with spaces.tmp 带空格的打印机名称
maybe this answer will help you:也许这个答案会对你有所帮助:

https://stackoverflow.com/a/6378038/20704455 https://stackoverflow.com/a/6378038/20704455

In short: double quote简而言之:双引号

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM