Passing argument surrounded with double quotes

c#, command-line, parameters

Solution

Not sure what solutions you've seen and tried but you need to escape the quotes

p.StartInfo.Arguments = "\"" + path + "\"";

or if you want to use the verbatim string literal (use `""` to escape)

p.StartInfo.Arguments = @""" + path + """;

If you have a lot of parameters, you might find the String.Format method easier to maintain.

p.StartInfo.Arguments = string.Format(@"""{0}""", path);

Problem

I've read many solutions to this problem and have tried all of them but cannot find the correct way to accomplish this task. My code is: ``` p.StartInfo.Arguments = path; ``` I need the path variable to be surrounded by " marks since it is a path to a file that has spaces in the directory names and file name. How can I put a " around the start and end of the path variable? Psudo code would be: ``` p.StartInfo.Arguments = DoubleQuote + path + DoubleQuote; ``` As a followup to this situation - once my .exe file received the path - the path was in its entirety following the "\"" suggestions. However, I had to enclose the path in the .exe file code in "\"" so it also could find the .xlsx file since the path and file name had spaces in them. Just wanted to followup with this for anyone else with this situation and wondering why the command line argument was ok, but the .exe file was not finding the file - both apps need enclosed in "\"".

Original source