In this tutorial you will learn how to create a simple Console-based Application which displays information about your PC using the "My" Namespace and Environment Variables!
Printer Friendly Download Tutorial (87KB) Download Source Code (9KB)
Step 1
Start Microsoft Visual Basic 2008 Express Edition, then select File then New Project... Choose Console Application from the New Project Window, enter a name for the Project and then click OK, see below:
Step 2
A Blank Module named Module1 should then appear, see below:
Step 3
With Module1 selected goto the Properties box and change the File Name from Module1 to modMain
Step 4
With the modMain Module Code View displayed, enter the following code in the "Sub Main()", subroutine:
Dim Output As String Output = "Computer Information" & vbCrLf & vbCrLf For Each Item As System.IO.DriveInfo In My.Computer.FileSystem.Drives If Item.IsReady Then Output = Output & "Drive:" & Item.VolumeLabel & "(" & Item.Name & ") Space " _ & Item.TotalFreeSpace & ") Size (" & Item.TotalSize & ")" & vbCrLf End If Next For Each Item As Environment.SpecialFolder In [Enum].GetValues(GetType(Environment.SpecialFolder)) Output = Output & "Special Folder:" & _ [Enum].GetName(GetType(Environment.SpecialFolder), Item) _ & " (" & Environment.GetFolderPath(Item) & ")" & vbCrLf Next Output = Output & "Memory (Physical):" & My.Computer.Info.AvailablePhysicalMemory & _ " of " & My.Computer.Info.TotalPhysicalMemory & vbCrLf Output = Output & "Memory (Virtual):" & My.Computer.Info.AvailableVirtualMemory & _ " of " & My.Computer.Info.TotalVirtualMemory & vbCrLf Output = Output & "Operating System:" & My.Computer.Info.OSFullName & _ " Version " & My.Computer.Info.OSVersion & vbCrLf Output = Output & "Computer:" & My.Computer.Name & vbCrLf Output = Output & vbCrLf & vbCrLf & "Environment Variables" & vbCrLf & vbCrLf For Each Item As DictionaryEntry In Environment.GetEnvironmentVariables Output = Output & Item.Key & ":" & Item.Value & vbCrLf Next Console.Write(Output) Console.Read()
See Below:
Step 5
Save the Project as you have now finished the application, then click on Start:
When you do the following will appear:
Step 6
Click on the Close button on the top right to end the application or press any key such as the Spacebar.
This is a very simple Console Application outputing information to a Console Window, the Console.Read waits for any input such as the Spacebar , this could be changed to show more or different data, console applications are useful for when you want to do something that does not require anything to been seen, it is up to you, make it your own!