Passing arguments to your VB.NET console application

Posted by Daniel - 28,955 Views

console application,vb.net,cli,command line interface,passing argument to vb.net,command line parameterA console application or a command line interface or CLI is a method of interacting with a computer via a text terminal. Commands are entered as lines of text (that is, sequences of typed characters) from a keyboard, and output is also received as text -wikipedia.org

To create a new console application project with Visual Basic.NET, choose File - New - Project. On the next dialog that appears, select Visual Basic Projects from the Project Types treeview then select Console Application icon from the Templates listview. Fill the project detail such as name and location then hit OK. A new module named Module1 will be created after that.

Inside the newly created module, there is a Sub procedure called Main(). This is the startup object of your console application, where you write your program code.

We will create a simple VB.NET console application which may accepts CLI parameters separated by commas. Below is the line that initiated an array to hold the parameters passed to the application:

Dim arrArgs() As String = Command.Split(“,”)

Lets say the name of your application is “myApp”. If you run it with this syntax below:

> myApp "Hello","World"

Then the arrArgs array will contains these element:

arrArgs(0) = "Hello"
arrArgs(1) = "World"

What you are going to do next with those values is up to you. Following is the Sub Main() full code you can use to test it:

Sub Main()
Dim arrArgs() A s String = Command.Split(“,”)
Dim i As Integer

Console.Write(vbNewLine & vbNewLine)

If arrArgs(0) <> Nothing Then
For i = LBound(arrArgs) To UBound(arrArgs)
Console.Write(“Parameter “ & i & ” is “ & arrArgs(i) & vbNewLine)
Next
Else
Console.Write(“No parameter passed”)
End If

Console.Write(vbNewLine & vbNewLine)
End Sub

That code will accept parrameters passed to the EXE then it will display the accepted parameters to the monitor.

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - January 2008


Leave a Reply


Options for your comment:





Hi, my name is Daniel Nugraha, a single male live on an island called Java, Indonesia. This is the place for me to share my interest in computer programming.

Coffee Break

Comments - Thanks Guys :)

  • Rangga Kusuma: Gan, Tengkiu buat postingan yang sangat berguna. Kebetulan ada project utk bikin sms gateway, dan converter Agan sangat berguna utk...
  • Chuck Norton: I actually went ahead & bolted over to Justin’s Get The Image plugin here: http://justintadlock.com/ar...
  • Chuck Norton: Question: is it possible to insert something like [custfieldimg=”joice1.jpg,15 0,1:1″] into the actual templates instead of...
  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.