简体   繁体   中英

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:\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. 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. 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

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

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"

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

        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.

    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.

Even

        "--somepath ",

breaks the parser of the receiving program.

If the process you want to start is 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
maybe this answer will help you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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