Solved

System.Diagnostics.ProcessStartInfo with string arguments

Posted on 2012-03-12
15
614 Views
Last Modified: 2012-03-12
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.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo
        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.
0
Comment
Question by:fantamen
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 37710166
Since you don't have specified the value of Par_Arguments value I think it is something like this:
Par_Arguments = "5     "

Use the following instead:
Par_Arguments = "5 \"     \""
0
 

Expert Comment

by:Stuporman
ID: 37710173
What does the assignment statement of Par_Arguments look like?
0
 

Author Comment

by:fantamen
ID: 37710416
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".
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Expert Comment

by:Stuporman
ID: 37710457
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.
0
 

Author Comment

by:fantamen
ID: 37710477
If I write
Par_Arguments = "2 \"Messaggio di prova\""
the compiler returns end of statements expected...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37710492
vb escape char is not \, it is "

try this:

Par_Arguments = "2 ""Messaggio di prova"""
0
 

Expert Comment

by:Stuporman
ID: 37710507
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"""
0
 

Author Comment

by:fantamen
ID: 37710907
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.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37710929
I think I already had this problem. try doubling them once more:


        Par_Arguments = "2 """"Messaggio di prova"""""
0
 

Author Comment

by:fantamen
ID: 37710945
Using your assignment the second parameter is evalueted partially, only until "Messaggio" and the substring " di prova" is discarded.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 37710963
can you try:


        Par_Arguments = "2 " + Chr(34) + "Messaggio di prova" + Chr(34)
0
 

Author Comment

by:fantamen
ID: 37711051
I've tried before opening the thread in expert exchange but 2 parameter was discarded e not recognized.
0
 

Author Comment

by:fantamen
ID: 37711547
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
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 37711745
Sincerely, i can't really but since you tested it and it is working properly, you should consider it as good.
0
 

Author Closing Comment

by:fantamen
ID: 37711881
To solve was necessary not only batch modifcations but your indications too. So thank you very match!.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question