.NET gives us the power to use a few nice functions CodeDom, ICodeCompiler and VBCodeProvider. Although ICodeCompiler is considered obsolete and users should be focusing on CodeDom I still prefer use ICodeCompiler.
There are two main ways to compile on the fly using the above functions and this is either through a source file on your hard drive or through a source or resource. I will show you options for both.
Compiling From File
This is a method I don't tend to use as it involves having the source on the user's PC and depending on what that source is I may not want it on there.
Add this code to a file and save it with a .vb extension. This code can be replaced with whatever you want to compile. Be careful though as it is strict compiling outside of the visual studio environment!
Imports System
Module Module1
Sub Main()
Console.WriteLine("Hello!")
Console.ReadLine()
End Sub
End ModuleI created a form with 2 textboxes, 1 called txtSource and 1 called txtOutput you should be able to work this out. I also added 2 buttons, a openfiledialog button called btnBrowse and the build button called btnBuild.
' CodeDom Compiler
' http://ProjectGhostt.com
' FreckleS
Imports System.CodeDom.Compiler
Public Class Form1
Private Sub Build(ByVal Output As String, ByVal Source As String)
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
Dim options As New CompilerParameters()
Dim results As CompilerResults
' Build Options.
options.GenerateExecutable = True
options.OutputAssembly = Output ' Name of server
options.ReferencedAssemblies.AddRange(New String() {"System.dll", "System.Data.dll", "System.XML.dll"})
results = Compiler.CompileAssemblyFromFile(options, Source) ' To compile from a local file
'results = Compiler.CompileAssemblyFromSource(options, Source) ' To compile from a source/resource
If results.Errors.Count > 0 Then
' Errors during compiling...
For Each CompilerError In results.Errors
MessageBox.Show("Error: " & CompilerError.ErrorText)
Next
ElseIf results.Errors.Count = 0 Then
MsgBox(txtOutput.Text & " was built!")
End If
End Sub
Private Sub btnSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click
Dim opf As New OpenFileDialog
opf.Title = "Select Source File"
opf.CheckFileExists = True
If opf.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtSource.Text = opf.FileName
End If
End Sub
Private Sub btnBuild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuild.Click
If txtOutput.Text = "" Then
MsgBox("Please enter a name!")
Else
Build(txtOutput.Text, txtSource.Text) ' To compile from a local file
'Build(txtOutput.Text, My.Resources.code) ' To compile from a source/resource
End If
End Sub
End ClassCompiling From Source
This in my opinion is a much better way to do things although it does make it a little harder to edit your code but it is still rather easy, you just lose intellisense.
Add a file to your resources called code.vb with your code, for testing purposes try this.
Imports System
Module Module1
Sub Main()
Console.WriteLine("Hello!")
Console.ReadLine()
End Sub
End ModuleI created a form with 2 textboxes, 1 called txtSource and 1 called txtOutput you should be able to work this out. I also added 2 buttons, a openfiledialog button called btnBrowse and the build button called btnBuild.
' CodeDom Compiler
' http://ProjectGhostt.com
' FreckleS
Imports System.CodeDom.Compiler
Public Class Form1
Private Sub Build(ByVal Output As String, ByVal Source As String)
Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
Dim options As New CompilerParameters()
Dim results As CompilerResults
' Build Options.
options.GenerateExecutable = True
options.OutputAssembly = Output ' Name of server
options.ReferencedAssemblies.AddRange(New String() {"System.dll", "System.Data.dll", "System.XML.dll"})
'results = Compiler.CompileAssemblyFromFile(options, Source) ' To compile from a local file
results = Compiler.CompileAssemblyFromSource(options, Source) ' To compile from a source/resource
If results.Errors.Count > 0 Then
' Errors during compiling...
For Each CompilerError In results.Errors
MessageBox.Show("Error: " & CompilerError.ErrorText)
Next
ElseIf results.Errors.Count = 0 Then
MsgBox(txtOutput.Text & " was built!")
End If
End Sub
Private Sub btnSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click
Dim opf As New OpenFileDialog
opf.Title = "Select Source File"
opf.CheckFileExists = True
If opf.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtSource.Text = opf.FileName
End If
End Sub
Private Sub btnBuild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuild.Click
If txtOutput.Text = "" Then
MsgBox("Please enter a name!")
Else
'Build(txtOutput.Text, txtSource.Text) ' To compile from a local file
Build(txtOutput.Text, My.Resources.code) ' To compile from a source/resource
End If
End Sub
End Class














