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.

Your blog is interesting!
Keep up the good work!
Hi Thank you very much this is very useful for the beginners. Keep it up
Cheers Damodhara !
That was really helpful.
hi, this is very useful. Thanks…..
it was a nice one clearing some basics.
thanks a lot to give me a start.
thank you very much, i have tried all your examples
best wishes
Thanks Zak. Glad it was helpful. Cheers
you’ve added value to my life. May God bless you. Will run it now.
@Muyiwa, you are welcome
Pls nicho, i later ran the program after configuring my path as U described. But the program didn’t run. I recieved a message “vbc is not recognized as an internal or external command”. Pls help me out.
@Muyiwa You must have made a mistake when setting up the compiler. are you using windows XP? Please go back to the section Configuring the Compiler and make sure you follow all the steps one by one. Then try again and let me know.
Cheers.
Great Nick, you are indeed wonderful. The program later ran. The problem was that i was using the wrong operating system. but when i tried on windows XP it ran. You are indeed a great mentor.
Pls. i have a flea for using VB.NET for solving mathematical problems, so i would appreciate any E book from you related to VB.net for engineers and scientist(For solving mathematical and engineering problems) My email address is fidelaurafurs@yahoo.co.uk
Appreciate all efforts by you.
Hi,
Such a nice and clean tutorial .. works great for me
It really helpful for beginers…thanks
Working on RPC-type calls, and now hope you monitor this.
I’ve got the remote calls to a .vbs working, but I’m not able to pass a parameter this way; need to pass parameters for the full script, as it will be running Quick Test Pro. As a demo, I’m using a simple test script.
Server-side Driver vbs:
strComputer = “Remote.Computer” ‘ You’d need to change this
strCommand = “c:\test.vbs one”
Const INTERVAL = “n”
Const MINUTES = 1
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)
Set objScheduledJob = objWMIService.Get(“Win32_ScheduledJob”)
Set objSWbemDateTime = CreateObject(“WbemScripting.SWbemDateTime”)
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now()))
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID)
If errReturn = 0 Then
Wscript.Echo “Test was started with a process ID: ” & intJobID
Else
Wscript.Echo “Test could not be started due to error: ” & errReturn
End If
*********
Client-side Proof of Concept VBS:
args = WScript.Arguments.Count
If args < 1 then
WScript.Echo "usage: args.vbs argument [argument] [argument] [argument] "
WScript.Quit
end If
WScript.Echo "You entered " & args & " Arguments, the first was "_
& chr(34) & WScript.Arguments.Item(0) & Chr(34)
*********************
I think I could arrange to test it using computer = "." for local confirmation, but since I've got the network and the script it will be passed to is already installed remotely, that seems like going backwards. Trying to get this to work "as desired" instead of "as scripted" and drawing on the Inet as my source code.
Any direction?
Thanks, nice tutorial, very helpful.
Must I have to uninstall VB9 to get it to stop opening the GUI when running cml line?
Oh well thanks for your time and effort, but I’ll figure out another way to learn cmd line coding/batch/vbs etc…
I’m sure this has something to do with coming to the computer after GUI’s were out and textbase was going out of style.
Hi
What do you thing about below diet supplement? I’m going to buy something good for muscle growth. Please give me a piece of advice.
[url=http://www.gaspari-nutrition.pl]Gaspari[/url]
You said that you haven’t tested it in Vista, so I did. I created my own Hello World program, added the path that contained the compiler to the environment variables list, and ran the program in command prompt. Works perfectly! Thanks for the tutorial!
very helpful thanks…
Nice answers in return of this matter with firm arguments and
telling all on the topic of that.
I like it whenever people come together and share ideas.
Great website, continue the good work!