So, Windows contains 2 hidden compilers (and also more) of importance , they are the Visual Basic (vbc) and C# (csc) compilers, they are generally located in the following folder:
C:\Windows\Microsoft.Net\Framework\
Find the corresponding folder for the latest version of .Net you have installed (v3.5 in my case)
Once you have found the latest folder, which should contain vbc.exe and csc.exe copy its path and press Win+D (Show the Desktop) now right click and create a shortcut, paste the path you copied and add \vbc.exe for the Visual Basic Compiler and click next, add an appropriate name and click finish and do the same for the C# compiler by copying the path and adding \csc.exe and an appropriate name and again pressing finish.
Now create a folder on the desktop called Sources (it can be called anything you like but this seems the most appropriate, and copy the following code sample into Notepad to first test the Visual Basic compiler:
Imports System
Module HelloWorld
Sub Main()
Console.WriteLine("Hello World")
Console.Read()
End Sub
End Module
Save the file in notepad as hello.vb in the folder you created (Sources), now open the folder and drag the hello.vb file onto the shortcut you created, you should find a file called hello.exe in the folder you created, double click the file. If it works you should see something similar to this (Click image to see larger version):
Congratulations, You just compiled your first VB app! ;-)
Now, you can do the same for C# too, copy the following code into Notepad to test the C# Compiler:
Class HelloWorld
{
Static Void Main()
{
System.Console.WriteLine("Hello World");
System.Console.Read();
}
}
Save the file as hellocsharp.cs and drag the file onto the C# compiler shortcut you created, a file called hellocsharp.exe should appear. Double click this file to test the file.
Now you've tested both compilers, and they should both work correctly. So, to recap, you now know how to use an in-built Visual Basic .Net and C# compiler(s) inside Vista and XP.
Below are some more code examples to try, mainly for VB.Net (vbc.exe):
Display 10 instances of Hello World
Imports System
Module HelloWorld
Sub Main()
Dim i as Integer
For i = 1 to 10
Console.WriteLine("Hello World")
Next
Console.Read()
End Sub
End Module
Display a Window with a Command Button and a Command Click event:
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public WithEvents button1 As Button
Public Sub New()
me.Text = "Hello World"
me.width = 230
me.height = 90
button1 = New Button()
button1.Size = New Size(200, 40)
button1.Location = New Point(10, 10)
button1.Text = "Hello World"
Me.Controls.Add(button1)
End Sub
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles button1.Click
MsgBox("Hello World", MsgBoxStyle.Information, "Hello World")
End Sub
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Alternative version, this version requires you to compile from the command line, so open the Command Prompt (Hit the Win key and type cmd on Vista, or Start > All Programs > Accessories > Command Prompt on XP) and paste the following (change v3.5 for the version of .Net you have (see start of tutorial) also newwin.vb is the name of the file, change this for the filename you used to save the code:
"C:\Windows\Microsoft.NET\Framework\v3.5\vbc.exe" newwin.vb /r:system.windows.forms.dll /r:system.dll /t:winexe /m:Form1
Ok, and so the code is below, save this file and replace the filename with newwin.vb to compile with the command above:
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public WithEvents button1 As Button
Public Sub New()
me.Text = "Hello World"
me.width = 230
me.height = 90
button1 = New Button()
button1.Size = New Size(200, 40)
button1.Location = New Point(10, 10)
button1.Text = "Hello World"
Me.Controls.Add(button1)
End Sub
Private Sub button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles button1.Click
MsgBox("Hello World", MsgBoxStyle.Information, "Hello World")
End Sub
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
OK, Thats it, At the top right corner there are links to VB.Net and C# tutorials, read them if you like, keep programming and enjoy the fact that you now have a VB and C# compiler "for free" and without any extra installations!
