Since the main difference between a console application and a Windows application in VB.NET is what gets loaded first, you can to "mix and match" console and Windows apps. For example, to start a program as a console application and then switch to Windows ...
- Add a module to your project - start with a Windows Application to generate the form automatically
- Add a Main method to that module
- Make the Main method the startup procedure in the Applications tab of the Project Properties window
- Make the project a Console Application in the same tab
- After whatever processing you want to do in the Command Prompt window, load the Form for your Windows application using this code:
Module DOSModule
Public Sub Main()
Console.Write("First, start with Command Prompt processing ...")
Dim myWinForm As New WinForm
Application.Run(myWinForm)
End Sub
End Module
Here's the result you should see:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
One of the reasons for writing console applications is to be able to use them in traditionally DOS based work such as system administration scripting. Here's how to call our example console application from a Visual Basic script (VBScript) ...
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Another combination of GUI and DOS is to call DOS applications directly out of a Windows GUI. There's still a lot of software that runs from a Command Prompt, especially utilities for system administration. One good example is the IPConfig network utility. If you work with technical support for a network problem, IPConfig is often the first utility they will ask you to run.
Calling another program from VB.NET involves some slightly more complicated code, however, and the use of a new VB.NET component: Process. Before VB.NET, it would have been necessary to call a Windows API to do most things like this. But like so much else, .NET provides a namespace that does the hard work for you.
In addition to being the namespace that we're going to use, "process" is also a technical term that you should understand. According to Microsoft:
"A process is a program that is loaded into memory and prepared for execution. Each process has a private virtual address space. A process consists of the code, data, and other system resources such as files, pipes, and synchronization objects that are accessible to the threads of the process."
The Windows Task Manager (Ctrl-Alt-Del) will show you the processes currently running on your computer.
Coding a GUI IPConfig
The Process component is the key to calling IPConfig from VB.NET. Process allows you to start, stop, and generally manage about anything associated with a Windows process. We're going to pass an argument to IPConfig by coding it in the Arguments property of an instance of Process. The CreateNoWindow property prevents the Command Prompt window from being displayed and the ReadToEnd method gets the output so we can display it with a VB.NET Label component. Here's the code (Button parameters not shown to keep the code shorter):
Imports System.ComponentModel
Public Class DisplayIPCFG
Private Sub RunIPCFG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunIPCFG.Click
Dim myProcess As New Process()
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
Try
myProcess.StartInfo.FileName = "ipconfig"
myProcess.StartInfo.Arguments = "/all"
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
TextBox1.Text = _
Replace(myProcess.StandardOutput.ReadToEnd(), _
Chr(13) & Chr(13), Chr(13))
myProcess.WaitForExit()
Catch ex As Win32Exception
MsgBox((ex.Message + ". Error Detected."))
End Try
End Sub
End Class
The end result is shown below:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
This is such a great idea that you wonder why Microsoft didn't think of it. Well ... they did. A GUI version of IPConfig isn't a standard part of Windows, but you can download it. Actually, theirs has a few more tricks, but nothing that you couldn't add to this program with a few minutes coding.

