Passing arguments to your VB.NET console application

Posted by Daniel - 21,201 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.

__________________________

Related posts brought to you by Yet Another Related Posts Plugin.

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:





Get my Full Feed Here or you can subscribe to one of my category based feeds below:
Coffee Break

Latest Blog Entries

Categories

Neighbours and Friends

Comments - Thanks Guys :)

  • Therese Lachance: Hi, Any idea how to have ContuttoPDF fetch the correct page language?
  • Marlena Albu: Super Blog, Dude! I am constantly on the watch for new and interesting sites and postings about audio equipment… which is what...
  • tresloukadu: yo how did u fixed when the tags shows <? and it shows < “& l t ; ” ?? please send me an email.
  • Sean: This is a great piece of code and thanks for adding the updates. Sean’s last blog post: Not All Text Message Marketing Is Created...
  • rodhy: Thank for your code, it very usefull for me. best regard.