Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, August 11, 2014 7:49 PM
If I want to pass a string literal with spaces as one of the arguments to the main [string [] args] I can encase the string in double quotes like so:
string`` my_arg ``=`` ``@"""C:\program files\my folder with spaces"""``;
However, how do I pass a string variable which contain spaces /literal combination as one of the arguments for a main method ?
The combination in question is for example the following where value is a string variable that contain spaces. The following doesn't work :
value + "text"
string`` my_arg2 ``=`` ""value + "text"""
and for that matter how do you just simply pass a string variable that contain spaces , this doesn't work where value is a string variable :
string`` my_arg2 ``=`` ""value"":
In context. I want to use the Process class to start an executable with arguments that contain spaces and then the started executable in turn passes on the arguments with spaces to another process that needs these arguments with spaces.?
Hope that makes sense.
thanks
All replies (8)
Monday, August 11, 2014 11:49 PM ✅Answered
ProcessStartInfo.Arguments = "\"" + arg1 + "\"" + " " + "\"" + arg2 + "\"";
Monday, August 11, 2014 7:54 PM
You use backslash to escape the double quote
string my_arg2 = "\"blaa\"";
will put double quotes round blaa in my_arg2.
A space in side a string is just another character and should be no problem, I have no idea what you mean about that.
Monday, August 11, 2014 8:17 PM
Thanks Andy. I was not very clear.
I need to start a process using Process/ProcessStartInfo
my.exe arg1 arg2 (both arguments contain spaces and they are string variables, not literals)
Which then starts another process and passes on both arguments containing spaces:
my2.exe arg1 arg2
The problem is that the 2 arguments that are passed as variables containing spaces become a lot more than two argument entries in the string array passed to the second process, obviously dependingon the amount of spaces in the string variables when passed to the second process. At least when using the following to start the first process.
ProcessStartInfo.FileName = my.exe
ProcessStartInfo.Arguments = arg1 + " " + arg2
that is in the second process main method :
args [0] does not equal arg1 that is passed to it because arg1 contain spaces.
Many thanks
Monday, August 11, 2014 9:41 PM
There's an arguments property.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx
Monday, August 11, 2014 10:05 PM
I am using the arguments property but it does not work for this scenario because one argument with spaces is turned into multiple arguments when passed as the string array input to the second process. That is the 2 string variable arguments (each string includes spaces) that are passed to the first process as "Arguments" are turned into a string array of 8 strings as input to the second process (executable with Main (String [] args). The number of strings in the input array to the second process main method obviously depends on the number of spaces in the initial 2 arguments passed.
I can add some unique characters to the end of the first argument passed to the first process so I can work out which of the eight strings actually tell me the file path which contain spaces. I thought there would be a better way.
Thanks
Tuesday, August 12, 2014 6:33 AM
I think you should refactor how this second process gets it's data (arguments).
If it's that complicated, put it in some xml file or database or something.
Tuesday, August 12, 2014 7:50 AM
That is the answer I'm looking for, many thanks Ante. Also thanks Andy
Tuesday, December 18, 2018 7:27 AM
ProcessStartInfo.Arguments = "\"" + arg1 + "\"" + " " + "\"" + arg2 + "\"";
You save my life...:)