1. Technology

Command Your Code!

Use Console Applications and the Command Prompt in VB 6 and VB.NET

By

Updated May 31, 2007

DOS is Forever!

I love to use that heading.

One About Visual Basic reader sent me an email telling me, "It's not DOS! DOS is dead! It's the Command Prompt!"

All too true! But it tickles me to see "essentially" the same environment that I used every day for so long still alive and well so many years later. To be more precise, the Command Prompt window is an emulation of DOS using the program CMD.EXE. (Tip: You can enter CMD in the Run ... textbox from the Start button and open a Command Prompt window faster than going through All Programs then Accessories.)

For example, VB.NET actually has much better support for applications designed to run at the Command Prompt than VB 6 had. We'll see proof of this in this article!

DOS Apps in VB 6

I wrote a summary of the ways to use DOS in VB 6 a few years ago in the article, VB 6 and DOS. This article details not only how to create a "DOS" application in VB 6, but also how to call the Shell function and use some traditional Windows API's from VB 6.

A VB 6 program executes just fine from a Command Prompt, but you have to compile it to an EXE. (That option is on the File menu in VB 6.) This VB 6 program opens a text file and displays it.

Because this site still supports VB 6, here's another trick you can use in the VB 6. Suppose you would like to open and read a simple file in VB 6. Here's standard code to do that: Command Prompt environment.

Private Sub Command1_Click()
Dim strValue As String
Open "d:\test.txt" For Input As #1
Do While Not EOF(1)
   Input #1, strValue
   MsgBox strValue
Loop
Close #1
End Sub

The result is shown in in this illustration:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

But suppose you want to pass a optional arguement with the file name. You can do that with the Command function in VB 6. Here's an example of the same program modified to allow you to specify a different input file using a command line argument or use the hard-coded file name in the program.

Private Sub Command1_Click()
Dim strValue As String
If Command = "" Then
   Open "d:\test.txt" For Input As #1
Else
   Open Command For Input As #1
End If
Do While Not EOF(1)
   Input #1, strValue
   MsgBox strValue
Loop
Close #1
End Sub

The result of this program is in this illustration:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

As usual, VB.NET adds more function. The Command function in both passes all of the command line arguments as a single string. If you have more than one, you have to parse the string yourself to get the individual arguments. Here's an example of some code in VB.NET that parses the Command function:

Module Module1
   Sub Main()
      Dim CommandStr As String
      Console.Write("Welcome to Console Applications" & vbCrLf)
      If Command() <> "" Then
         Dim ArgCounter As Integer = 1
         CommandStr = Command()
         Do While (CommandStr.IndexOf(" ") > 0)
            Console.Write("Here is argument number " & _
               ArgCounter & " " & _
               Left(CommandStr, CommandStr.IndexOf(" ")) & vbCrLf)
            CommandStr = Right(CommandStr, CommandStr.Length - _
               (CommandStr.IndexOf(" ") + 1))
            ArgCounter += 1
         Loop
         Console.Write("Here is argument number " & _
            ArgCounter & " " & CommandStr)
      End If
   End Sub
End Module

But But VB.NET adds My.Application.CommandLineArgs and that makes doing the same thing much simpler.

Module Module1
   Sub Main()
      Console.Write("Welcome to Console Applications" & vbCrLf)
      Dim ArgCounter As Integer = 0
      For Each argument As String _
         In My.Application.CommandLineArgs
         ArgCounter += 1
         Console.Write("Here is argument number " & _
            ArgCounter.ToString & vbCrLf)
      Next
   End Sub
End Module

In either case, you get an identical result:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Both VB 6 and VB.NET provide debugging support for Command Prompt arguments by allowing you to enter them into a text box in the project Properties window. The article VB 6 and DOS shows you how to do it.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Command Your Code!

©2014 About.com. All rights reserved.