How to process Command Arguments with spaces and ""s?

J

Joe Duchtel

Hello -

I have an application for which I need to pass in a path as command
argument. Neither of the following two statements will not work as
the command argument is split at the space ...

Dim lCommandArgument() as String = Microsoft.VisualBasic.Command()
lComandArgument(0)

My.Application.CommandLineArgs(0)

So a "C:\Document and Settings" results in "C:\Document".

Is there any way to make this work? I cannot use the Sub Main(ByVal
aArguments() As String) as I use a Form.

Would anybody have a slick regular expression or anything else that
will parse for the double quotes and treat everything enclosed as a
single argument?

Thanks!
Joe
 
F

Family Tree Mike

Hello -

I have an application for which I need to pass in a path as command
argument. Neither of the following two statements will not work as
the command argument is split at the space ...

Dim lCommandArgument() as String = Microsoft.VisualBasic.Command()
lComandArgument(0)

My.Application.CommandLineArgs(0)

So a "C:\Document and Settings" results in "C:\Document".

Is there any way to make this work? I cannot use the Sub Main(ByVal
aArguments() As String) as I use a Form.

Would anybody have a slick regular expression or anything else that
will parse for the double quotes and treat everything enclosed as a
single argument?

Thanks!
Joe

It sounds like the problem is how you created the call to the
executable, not the way you are reading the arguments in the code. To
pass something in with spaces, surround it with quotes. To include a
double quote as part of a string, put double quotes twice.

So, this call:

someprog.exe "c:\program files\myapp" "A string with a "" double quote"

would have two arguments, the second having both spaces and double
quotes internally.
 
J

Joe Duchtel

It sounds like the problem is how you created the call to the
executable, not the way you are reading the arguments in the code.  To
pass something in with spaces, surround it with quotes.  To include a
double quote as part of a string, put double quotes twice.

So, this call:

someprog.exe "c:\program files\myapp" "A string with a "" double quote"

would have two arguments, the second having both spaces and double
quotes internally.

Hello -

Thanks for the response! I did use double quotes but somewhere while
I was testing I screwed up and didn't try the
My.Application.CommandLineArgs(0) when I passed in the argument(s)
that way. It's working just fine now!

Thanks again,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top