| You are here: | About>Computing & Technology>Visual Basic |
![]() | Visual Basic |
Command Your Code!May 30 2007 Use Console Applications and the Command Prompt in VB 6 and VB.NETDOS 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() The result is shown in in this illustration: -------- 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() The result of this program is in this illustration: -------- 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 But But VB.NET adds My.Application.CommandLineArgs and that makes doing the same thing much simpler. Module Module1 In either case, you get an identical result: -------- 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. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


