Frekster
Frekster

Reputation: 1148

Passing argument surrounded with double quotes

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

Upvotes: 2

Views: 8896

Answers (2)

JaredPar
JaredPar

Reputation: 755457

You just need to append the double quote character to the start and end of the string. Creating the double quote can be done in either of the following ways

  • "\""
  • @""""

Upvotes: 0

keyboardP
keyboardP

Reputation: 69372

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

Upvotes: 7

Related Questions