fantamen
asked on
System.Diagnostics.ProcessStartInfo with string arguments
I've to pass to a cmd file 2 arguments: the first is a number and the second is a description string containing spaces.
My code looks like this ...
Dim psi As System.Diagnostics.Process StartInfo = New System.Diagnostics.Process StartInfo
psi.FileName = "C:\Test.cmd"
psi.Arguments = Par_Arguments
psi.WindowStyle = ProcessWindowStyle.Normal
System.Diagnostics.Process .Start(psi )
Par_Arguments is the variable that contain the 2 arguments.
My trouble is what I have to do to build the second string argument with spaces.
My code looks like this ...
Dim psi As System.Diagnostics.Process
psi.FileName = "C:\Test.cmd"
psi.Arguments = Par_Arguments
psi.WindowStyle = ProcessWindowStyle.Normal
System.Diagnostics.Process
Par_Arguments is the variable that contain the 2 arguments.
My trouble is what I have to do to build the second string argument with spaces.
What does the assignment statement of Par_Arguments look like?
ASKER
The value of Par_Arguments can be for example "2 Messaggio di prova" where the first parameter is "2" and the second "Messaggio di prova".
In that case, petr_hlucin's answer is what you need. it would look like this:
Par_Arguments = "2 \"Messaggio di prova\""
The reason you need this is that Windows splits your argument string by spaces, so your raw text gets passed as 4 different arguments instead of two. That means you need to surround the second argument with quotes, and the only way to do that in VB is to use the escape sequence for quotes, which is \".
Buona Fortuna, mi amici.
Par_Arguments = "2 \"Messaggio di prova\""
The reason you need this is that Windows splits your argument string by spaces, so your raw text gets passed as 4 different arguments instead of two. That means you need to surround the second argument with quotes, and the only way to do that in VB is to use the escape sequence for quotes, which is \".
Buona Fortuna, mi amici.
ASKER
If I write
Par_Arguments = "2 \"Messaggio di prova\""
the compiler returns end of statements expected...
Par_Arguments = "2 \"Messaggio di prova\""
the compiler returns end of statements expected...
vb escape char is not \, it is "
try this:
Par_Arguments = "2 ""Messaggio di prova"""
try this:
Par_Arguments = "2 ""Messaggio di prova"""
I'm sorry, I was confused with C#. To escape quotes in VB, use two in a row like this:
Par_Arguments = "2 ""Messaggio di prova"""
Par_Arguments = "2 ""Messaggio di prova"""
ASKER
Now code is compiled correct but I'm investigating for a bad response from .cmd file.
It run correctly only if second parameter don't contain spaces.
It run correctly only if second parameter don't contain spaces.
I think I already had this problem. try doubling them once more:
Par_Arguments = "2 """"Messaggio di prova"""""
Par_Arguments = "2 """"Messaggio di prova"""""
ASKER
Using your assignment the second parameter is evalueted partially, only until "Messaggio" and the substring " di prova" is discarded.
can you try:
Par_Arguments = "2 " + Chr(34) + "Messaggio di prova" + Chr(34)
Par_Arguments = "2 " + Chr(34) + "Messaggio di prova" + Chr(34)
ASKER
I've tried before opening the thread in expert exchange but 2 parameter was discarded e not recognized.
ASKER
Good. Now probably I've found the reason: in batch file there is this line:
if "x%1"=="x" goto Missing_Severity
I've replaced with
if [%1]==[] goto Missing_Severity
... and now all works correctly.
Can you confirm my change is correct?
Thanks
if "x%1"=="x" goto Missing_Severity
I've replaced with
if [%1]==[] goto Missing_Severity
... and now all works correctly.
Can you confirm my change is correct?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To solve was necessary not only batch modifcations but your indications too. So thank you very match!.
Par_Arguments = "5 "
Use the following instead:
Par_Arguments = "5 \" \""