How to pass cmdline args to a Windows App?

T

Tom Bower

I have a simple one-form Windows App I'm working on in
VB. I need to be able to pass some command-line
parameters to the application in order for it to do what
it needs to do when it fires up.

Any pointers on how to do that?
 
H

Herfried K. Wagner [MVP]

* "Tom Bower said:
I have a simple one-form Windows App I'm working on in
VB. I need to be able to pass some command-line
parameters to the application in order for it to do what
it needs to do when it fires up.

\\\
Public Sub Main(ByVal astrCmdLineArgs() As String)
Dim i As Integer
For i = 0 To astrCmdLineArgs.Length - 1
Console.WriteLine(astrCmdLineArgs(i))
Next i
End Sub
///

- or -

\\\
Public Sub Main()
Dim i As Integer
For i = 0 To Environment.GetCommandLineArgs().Length - 1
Console.WriteLine(Environment.GetCommandLineArgs(i))
Next i
End Sub
///

In order to test the samples above, you must set the application's
startup object to 'Sub Main'. The 2nd sample code can be used at any
place inside a Windows Forms application too.
 
T

Tom Bower

Hey, that's cool, they're right there. And I even
figured out that you can pass sample ones using the
debugger by setting them in the Project's Debugging
Properties page.

Thanks, this forum is very helpful.
 
M

Mattias Sjögren

The 2nd sample code can be used at any
place inside a Windows Forms application too.

But it has the downside of requiring EnvironmentPermission read access
to the %PATH% variable, which might not be granted in a partially
trusted environment.



Mattias
 

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