HowTo: Command Line Arguments with VB.Net CF

  • Thread starter Benjamin Lukner
  • Start date
B

Benjamin Lukner

Hi!

The above question has been asked several times.
But there has no "complete" solution been posted (in the last 12
months), so I thought I could do it because I had the problem myself
this week (sample code follows below).

1)
If you use Microsoft.VisualBasic.Interaction.Command() or
Microsoft.VisualBasic.Command(), you will get a NotSupportedException.
That is very nasty because it compiles without errors and the exe works
fine under Windows XP (but not under Windows CE).

2)
So we'll use the code known from e.g. C++ to start our app (string
args[]). We get a string array of the command line arguments separated
at each space (if you pass the parameters without quotation marks).
Problem: This only works with Sub Main, not with a form.

3)
So we have to call the form from the Sub Main. But when Sub Main is
left, the whole program stops and the form closes. So we have to run the
form via System.Windows.Forms.Application.Run (what I didn't know till now).

4)
Simply extend the Sub New of the form or insert another Sub New to pass
the command line arguments.

5)
To run the program with command line parameter, simply create a .lnk to
the exe and open it with a text editor. Then add the command line
parameter to the call of the exe file there, e.g.:

29#"\Temp\Test.exe" Each word in a single line "Another one line test"


_Sample code_:


Public Class Class1
Public Shared Sub Main(ByVal args() As String)
System.Windows.Forms.Application.Run(New Form1(args))
End Sub
End Class

Public Class Form1
'[...]
Public Sub New(ByVal args() As String)

Me.New() ' Call original Sub New

Dim i As Integer

If Not args Is Nothing Then
For i = args.GetLowerBound(0) To args.GetUpperBound(0)
MsgBox(args(i))
Next
End If

End Sub
'[...]
End Class


Kind regards,

Benjamin Lukner
 

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