Use Process.Start with parameters AND spaces in path

WEFX picture WEFX · Jun 26, 2013 · Viewed 64.4k times · Source

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?

Answer

Abdul Saleem picture Abdul Saleem · Jan 12, 2015

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.