ARTICLE

Access Command Line Arguments in VB.NET

Posted by Mahesh Chand Articles | Visual Basic 2010 February 05, 2007
This article shows how to access command line arguments in VB.NET.
Download Files:
 
Reader Level:

Recently, one user asked me how to access command line arguments in a VB.NET application. This how do I answers the same.

There are two common ways to read command line arguments in VB.NET. First, you can override the Main method with an array of strings, which are command line arguments. For example, the following code loops through the command line arguments and print them on the console.

Shared Sub Main(ByVal args As String())

    For Each arg As String In args

        Console.WriteLine(arg)

    Next arg 

    Console.ReadLine()

End Sub

I compile the above code and run the exe from the command line by passing following parameters:



The output generated by the application looks like following:



However, this is not only the way to read command line arguments. For example, what if you do not want to override the Main method? Or access the command line arguments from non-Main method of your application?

You can do so using the Environment class, which has a static method called GetCommandLineArgs, which returns an array of strings containing the arguments. The following code reads the command line arguments using Environment.GetCommandLineArgs method.

For Each arg As String In Environment.GetCommandLineArgs()

    Console.WriteLine(arg)

Next arg

Download and run the attached code for more details.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 

Why is there no shared sub on console application in Main()? i done it by using a class... is it ok? but cannot accept an array... need help..thanks

Posted by Bensyl Domingo Dec 27, 2010

hello i want to create some kind of program enable to switch off my monitor when i want rather than waiting for windows to do it by its preset settings in the power saving options which is of fixed time intervals

Posted by kevin Oct 07, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor