In .Net you can easily write full and powerful programs using a simple text editor like notepad, and execute the programs without any problems.You can also easily run the programs with command line arguments.In this post I will show you how to configure your system, and the command prompt, so that you can write your programs in Notepad, compile and run them at the command prompt, and additionally how you can run the programs with command line arguments.
Since I haven’t completely moved to C# yet (still getting my feet wet with the language), the code samples will be in VB.Net. But for the C# programmers, you can easily convert the code and make it work.
Some advantages of using command line aruments are first you write fewer lines of code, and second it saves you the time, from say program start > UserInput > execute to program start with input >execute.
For example to run a simple addition program.You click start>run> C:\Add.exe 12 12
Configuring the compiler.
The compiler used to create Visual Basic applications is called vbc.exe and it is installed by default in the path, Drive:\Windows\Microsoft.Net\Framework, however you can’t just use it from there. To use it for all your programs, you need to include it in your system path.I will show you how to include it below.This works for Windows 2000/XP/2003. I haven’t tested it against Vista.
-
- In windows explorer, locate the folder where the compiler is installed, example, mine is C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
- If you have different .Net Frameworks installed, you will see all of them in the folder ..\Framework. Choose the appropriate framework and double click to open its folder. For this post we’ll use . Net Framework 2.0
- Copy the path.
- Click Start>Control Panel and then Double Click on System.
- Click the advanced tab on the System Properties dialog box, and then click Environment Variables.
- Under the System Variables Section, click Path and then click Edit.
- Check if the path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 already exists.If it doesn’t, move to the end of the string, type ; then paste the path you copied earlier in step 3.
- After this step, mine looks like this
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
- Click OK to close the dialog box, and click OK to the remaining of the dialog boxes.
- Congratulations, you are done.
Now we are going to write our program , compile it using the compiler we just configured and execute it. The program will display some text to the console window.
Let’s write some code.
Start Notepad and type in the following code
Imports System
Module Sample
Sub main()
Console.Writeline("Hello there, welcome!")
Console.Write("Press Enter to close the program")
Console.Read()
End Sub
End Module
Click File>Save As and type Sample.vb
Under the Save in box, browse to the C:\ directory
Under Save as Type, make sure to select All Files.
Right Click in the window and select New>Folder. Type Test and then Double click to open the new folder. Click Save to save the program in this folder.
How to Compile and run the program
Now we are going to compile and run our program. Click Start > Run then type cmd and click OK to open the windows command prompt.
We need to change the directory to the location of our .vb file in order to compile it. To change the directory, under the command prompt type cd\ and press Enter.
Type again cd Test and press enter.You are now in the directory with our program.
To compile the program type the following: vbc Sample.vb and then press enter. If the program compiles without any errors you will see the following text displayed and a command prompt. If an error message is displayed, go back to Notepad and make sure you wrote the code correctly.
C:\Test>vbc sample.vb
Microsoft (R) Visual Basic Compiler version 8.0.50727.1433
for Microsoft (R) .NET Framework version 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
C:\Test>
Now at the command prompt, type sample and press enter.You will see output as in the image below.

Congratulations. You have successfully created, compiled and executed a simple application by just using Notepad and the command prompt. If you browse to the Test folder, you will see that a new file has been created with the name sample.exe.That is our executable program. To call it from the command prompt, you can ommit the .exe and just type sample.
Command Line Arguments
Now we are going to modify our program to accept command line arguments, and do something with them.
Change the source code as shown below.
Imports System
Module Sample
Sub Main(Byval args() As String)
Dim data As String = args(0)
Console.Writeline("Your input was: {0}", data)
Console.Write("Press Enter to close the program")
Console.Read()
End Sub
End Module
Let me explain the basics. The programs accepts arguments as an array – args() of strings. This args() parameter is handled by the system, and it accepts any arguments you will need to supply to the program. All you have to do is add some code to work with the string parameters or parse them into integers or other data types that you may want to work with.
Save the changes you made to the sample.vb file.
Go back to the command prompt and type vbc sample.vb and then press Enter.(You need to re-compile the program every time you make any changes.)
At the command prompt, type sample.exe “Hello there” and press enter. The program will run and you will see the output in the window.
Your input was: Hello there
Press Enter to close the program
The easy way.
Now click Start > Run
Type C:\Test\Sample.exe “Hello and welcome”
Click ok
The program will run and show your input to the console window.
Multiple arguments.
Now let us modify the program to accept two arguments. First we will create a simple procedure that adds two numbers, and then launch our program with the two numbers to be added as arguments. Modify the code to the following.
Imports System
Module Sample
Sub main(Byval args() As String)
Dim first As Integer = Cint(args(0))
Dim second As Integer = Cint(args(1))
Total(first,second)
Console.Write("Press Enter to close the program")
Console.Read()
End Sub
Sub Total(Byval first As Integer, Byval second As Integer)
Console.Writeline("The total is: {0}", first+second)
End Sub
End Module
Save the file, switch to the command prompt, compile the program and then minimize the command prompt.
Click Start > Run and type C:\Test\sample.exe 12 12
The program will run and display the following output to the console window.

Note that if you have written code to work on two or more parameters, you must supply all the parameters otherwise your program will generate an error at runtime. For simplicity in this sample I did not add any error handling code. You can add try catch blocks to display appropriate error messages when you write your programs.
There are many things you can achieve by launching a program with command line arguments. You can supply a file path and perform some action against the file etc.This post only scratches the top, giving you the basics and showing you that like with C/C++ even with VB.Net, you can successfully create programs that accept command line arguments.
Well again that is it for now. I hope you got the idea on how to create VB.Net programs that accept command line arguments. If you have any questions or need any additional info, please add your comments below or email me through n i c l i v e @ g m a i l dot c o m.
Tags: Coding, command prompt, vbc compiler