Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid that a string splits on every whitespace in command line

Tags:

c#

I'm writing a program in which the user can type in some informations about a customer and then open a MS Word model (*.dotx). After that he can directly archive it with another program. So I click on a button which I created for MS Word and then it should open the other program (the archive program) and pass the path to the *.dotx file to it. I got this code to pass the path and open the archive program:

Process p = new Process();
p.StartInfo.Arguments = "Word " + secondArgument;
p.StartInfo.FileName = fileName;
p.Start();

The string secondArgument ist the path to the file and fileName is the path to the exe file of the archive program.

To get the arguments in the archive program, I use this code in Form_Load():

string[] args = Environment.GetCommandLineArgs();

Then I use a MsgBox to look if it's correctly passed. But it isn't. The name of the .dotx file has whitespaces in it (e.g. "path\This is a test file.dotx"). So the output of MessageBox.Show(args[0]) is "path\This". How can I avoid that it splits at every whitespace?

Suggestions appreciated :)

like image 777
Tyler Avatar asked Apr 25 '14 11:04

Tyler


1 Answers

You need to surround it with quotation marks:

 "This is a test file.dotx"

MSDN:

Command line arguments are delimited by spaces. You can use double quotation marks (") to include spaces within an argument.

like image 57
Tim Schmelter Avatar answered Nov 10 '22 18:11

Tim Schmelter