none
how to pass multiple arguments with white space inside to ProcessStartInfo RRS feed

  • Question

  • I have a command line tool to generate some file, it has multiple arguments, some arguments have white space in the path, 

    first I define a template string to be formatted, looks like below:

    private string TEMPLATE_STR= "/C \"{0}Tool_v1031\\ToolC\" -sPDF=\"{1}\" -sLIC=\"{2}\" -oPDF=\"{3}\" -dType=0 -oAAD=\"{4}\" -pStart={5} -pEnd={6}";
    string strCmd = string.Format(TEMPLATE_STR, System.AppDomain.CurrentDomain.BaseDirectory, pdf, lic, outfile, aadfile, start, end);
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = strCmd;
    process.StartInfo = startInfo;
    process.Start();
    process.WaitForExit();
    result = process.ExitCode;
     

    the arg {0} is a folder including whitespace, {1}, {2}. {3}. {4} are file path which all have whitespace inside, when I run the code, the result always 1, but when I copy/paste the strCmd to cmd.exe and run it manually (without prefix /C), it works!

    I almost spend 5 hours on it with no luck, could someone help please!

    Tuesday, August 29, 2017 5:16 PM

Answers

All replies

  • Try with /K option instead of /C and also do not specify the Hidden style. When it stops at WaitForExit, examine the window, maybe there are some useful messages.

    Tuesday, August 29, 2017 5:52 PM
  • Thanks for your help, below is the whole string of startInfo.Arguments:

    "/K \"C:\\Program Files\\Shufa\\Tool_v1031\\ToolC\" -sPDF=\"C:\\Users\\James\\AppData\\Local\\Shufa\\downloads\\packages\\temp\\aaa\\readme_ap.pdf\" -sLIC=\"C:\\Program Files\\Shufa\\pl_A4_20A.xml\" -oPDF=\"C:\\Users\\James\\Documents\\Shufa\\aaa_dot1.pdf\" -dType=0 -oAFD=\"C:\\Users\\James\\AppData\\Local\\Shufa\\temp\\readme_ap.aad\" -pStart=0 -pEnd=1"

    the cmd.exe window has below error information:

    'C:\Program' is not recognized as an internal or external command,
    operable program or batch file.

    I think the first parameter with double quote has whitespace cause this error. why the double quotes does not work?

    Wednesday, August 30, 2017 3:17 AM
  • According to “cmd/?”, try this:

    "/S /K \"\"C:\\Program Files\\Shufa\\Tool_v1031\\ToolC\" . . . -pEnd=1 \""

    Wednesday, August 30, 2017 5:02 AM
  • Great, works like a charm!

    I use "/C \"\"C:\\Program Files\\Shufa\\Tool_v1031\\ToolC\" . . . -pEnd=1 \""

    Thanks Viorel_!

    Friday, September 1, 2017 8:26 AM