Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - passing arguments with space in between them to a process

Tags:

c#

I'm trying to pass an argument to a process a folder with space in its name. It doesn't recognize the folder. How can i do that?

string my_arg = @"C:\\program files\\my folder with spaces";

ProcessStartInfo proc = new ProcessStartInfo();

proc.FileName = @"C:\batches\my_batch.bat";

proc.Arguments = @my_arg ;

Process.Start(proc);

the process wont start - it does work if i use a folder with no spaces in the name. Thank you!

like image 398
pall Avatar asked Jul 03 '11 10:07

pall


2 Answers

You’re using literal strings; there is no need to escape the backslashes, and indeed if you do then there’s no need to use a literal string in the first place.

The spaces on the other hand require special care – encase the argument into quotes solves this.

string my_arg = @"""C:\program files\my folder with spaces""";
like image 51
Konrad Rudolph Avatar answered Sep 18 '22 11:09

Konrad Rudolph


try this:

string my_arg = "\"C:\\program files\\my folder with spaces\"";
like image 25
kleinohad Avatar answered Sep 16 '22 11:09

kleinohad