The Blog of a Programmer
Archive for July 11, 2006
Visual Basic.net Console Applications
Jul 11th
Microsoft Adds Improved Support for Console Apps in .Net
When I learned that .net had improved support for console apps I became immediately excited about the whole thing. I love data processing, parsing, and conversion; I’m a geek in all every possible sense of the word. It can be a true mental challenge to convert something one company believes to be a complete and sensible data format to what another company believes to be the same.
In this past week I came across the need to build a program which will be capable of automation and quickly converting one companies file format to another.
O’ The Possibilities
Since I am mostly a web based developer my immediate thought was VBScript (Classic ASP) hosted on an IIS capable server on which we would have a batch calling ftp to download the files which needed to be parsed, saving them to a specific folder, and executing the .asp script.
Unfortunately the server which would be running the program (decided on by the client company) does not run IIS, so there goes that idea. Another idea was to run it as a .vbs script using windows internal scripting. I’ve always had a bad feeling about using those files and decided that would not be a grand idea. Out of a gut feeling I scratched that idea.
What about .net?
I had worked with VB.net only a few times before and thought it would be easy enough to do the job with. What about automation? Having a form without need of human intervention would allow the program to run by itself BUT why do I even need that? It would make the program larger and that is completely unnecessary. The next thought that came to my mind was, “I miss DOS.”
DOS and Console Applications!
Most of you past the age of 20 or so should remember console apps. Those DOS console windows, the constant use of “cmd” or “command” from your start —> run menu, and (for those of us who like to play those old dos games) free dos or DOSBox! Either way, those wonderful apps didn’t just become useless or out of style with the advent of easily built apps with nifty GUI’s!
Console apps are great for processing and well automating processes! Which is how I am using my newest little application.
How to Write Console Apps with .Net 2003Okay first off, I know .net 2005 is out. I’m currently using .net visual studio 2003, and although the process should be similar in 2005, perhaps even better; I’m using 2003 and that’s what you’ll learn here. That is, until I get upgraded.So open up your .net enviroment.
Create a new project selecting Visual Basic Projects (left hand side), and Console Application (right hand side). Name your application and have it save to whatever location you’d like.
Module1.vbIf you are anything like me you’ll love the fact that the first thing you see after creating this new application is a code window! None of the GUI or form clutter. Straight to the heart of the matter! Now unless your application is completely static, as in it always processes the same file(s), you’ll need your program to handle arguments. In my case it needs to handle 1 argument and that would be the file name of the text file to be parsed.Handling Arguments
Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Wonderful little piece of code! Since this is the first function executed in your program all it will do is collect the arguments from the command line and pass them into a private sub also called Main in your application, in which you can check for the correct arguments and actually do your processing! Here is a basic example:
Private Sub Main(ByVal arguments() As String)
If arguments Is Nothing OrElse arguments.Length <> 2 Then
Console.WriteLine(“You must enter atleast one argument for this program to execute!”)
Else
System.Console.WriteLine(“–==Welcome to My Console Application==–”)
If (arguments(1) = “-V”) Then
System.Console.WriteLine(“–==Written by JuanJose H. Galvez==–”)
Exit Sub
Else
Dim user_name
System.Console.WriteLine(“What is your name?”)
user_name = System.Console.ReadLine()
System.Console.WriteLine(“Your name is: ” & user_name)
End If
End If
End Sub
From the above example you should be able to figure out how to initiate variables, set user input into a program usable variable, how to write out strings to the screen! Also, you’ll either run into this or figure it out yourself but arguments(0) is actually equal to the filename of the program, which is why the argument(1) is used when seeking the input argument used to run the program.
Example: “ConsoleApplication1.exe” -V
Example: “ConsoleApplication1.exe” AnythingCanGoHere
This is obviously a useless program!
You’ve wasted my time!Actually, this little program was indeed a useless example, in that it does nothing of use; thus the use of the term useless; but it does end up teaching someone who is new to console applications how to get started. It also teaches the basic concepts behind arguments and how to do basic input output operations between the program and the end user.And you wasted your own time reading this! I wasted time writing! :)
Recent Comments