How to use the CodeDom Compiler
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
Check this project out before you start reading this tutorial. It is an example of what you can use this tutorial to make.
I don't know if its posted before, but ill post this anyway.
Its nice to be able to make programs with your own programs. You can customize it how you want and it dont need to have that big file size like VB Express or VS. So im going to learn you how to use:
To begin with...
Import this namespace at the top of your code:
I don't know if its posted before, but ill post this anyway.
Its nice to be able to make programs with your own programs. You can customize it how you want and it dont need to have that big file size like VB Express or VS. So im going to learn you how to use:
- Create a Compiler
- Compiler Options
- Adding References
- Adding Resources
- Show installed Languages
- Compile to Assembly
- Example
To begin with...
Import this namespace at the top of your code:
Code: Select all
Inside your class, you need to declare these variables
Imports System.CodeDom.Compiler
Code: Select all
In this tutorial we will be using Options as CompilerParameters and Provider as a CodeDomProvider Private provider As CodeDomProvider
Private Options As New CompilerOptions
You do not have the required permissions to view the files attached to this post.
Last edited by MrAksel on Sun Mar 06, 2011 6:01 pm, edited 17 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

To create a compiler, you must choose what language that shall be used, for example VB, CS, CPP...
To do this use this code:
Top
To do this use this code:
Code: Select all
Of course you can change "vb" to whatever coding language you want, but we will come back to that later in Show installed Languages.If CodeDomProvider.IsDefinedLanguage("vb") Then
provider = CodeDomProvider.CreateProvider("vb")
'We will use this provider in our code later
Else
MsgBox("The specified language is not defined for this system.")
End If
Top
Last edited by MrAksel on Sun Mar 06, 2011 12:39 pm, edited 3 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

We will use the class CompilerParameters to set the options for our compiler
The available options is:
The available options is:
- CompilerOptions Paramaters for compiling, for example "/optimize"
- EmbeddedResources The .NET Resource files for embedding when compiling
- GenerateExecutable If true, generates an executable assembly(EXE), not a library(DLL)
- GenerateInMemory If true, generates the compiled output in memory
- IncludeDebugInformation Specifies whether to create debug information for the executable file
- LinkedResources The .NET Resource files that is referenced in the source
- MainClass The main, or startup class
- OutputAssembly The output file
- ReferencedAssemblies Assemblies referenced by the current project, for example "System.dll"
- TempFiles Temporary files for the compilation
- TreatWarningsAsErrors Gets or sets a value indicating whether to treat warnings as errors.
- UserToken Gets or sets the user token to use when creating the compiler process.
- WarningLevel Gets or sets the warning level at which the compiler aborts compilation.
- Win32Resource Gets or sets the file name of a Win32 resource file to link into the compiled assembly.
Last edited by MrAksel on Sun Mar 06, 2011 11:52 am, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

To add references to your project, use the ReferencedAssemblies property. This example demonstrates how to use this property:
Code: Select all
TopDim OpenReferences As New OpenFileDialog
OpenReferences.Title="Add References"
OpenReferences.MultiSelect=True
If OpenReferences.ShowDialog() = DialogResult.OK Then
Options.ReferencedAssemblies.AddRange(OpenReferences.FileNames) 'Adds a range of files selected in the file dialog
End If
Last edited by MrAksel on Fri Nov 18, 2011 11:00 am, edited 2 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Resources is a group of files compiled into a .NET Resource file. The can be images, sounds, text, or files. They are used by the program in different ways. One way is to display an image in a picture box, another is to change the language of the application. The EmbeddedResources property is mostly files or objects used in the application, while the LinkedResources can be culture options, these will be included in a separate assembly. To check if the assembly supports resources, use this piece of code:
Top
Code: Select all
To add a resource file, or more use this:
If Provider.Supports(GeneratorSupport.Resources) Then
'Add resource files here
End If
Code: Select all
You can change 'EmbeddedResources' to 'LinkedResources' if you want the resources to be linked in another assembly.Dim OpenResources As New OpenFileDialog
OpenResources.Title="Add Resources"
OpenResources.MultiSelect=True
OpenResources.Filter = "Resource files(*.resource)|*.resource"
If OpenResources.ShowDialog() = DialogResult.OK Then
Options.EmbeddedResources.AddRange(OpenResources.FileNames) 'Adds a range of files selected in the file dialog
End If
Top
Last edited by MrAksel on Sun Mar 06, 2011 11:52 am, edited 2 times in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Its handy to know which coding languages that is supported on your computer, here is some code to show it:
Top
Code: Select all
This code will show you a message box with each language on its own line. You can replace "vb" in Create a Compiler with one of these languages.Dim Languages As String = ""
For Each language As CompilerInfo In CodeDomProvider.GetAllCompilerInfo
For Each codingLanguage In language.GetLanguages()
Languages &= codingLanguage & vbNewLine
Next
Next
MsgBox(Languages)
Top
Last edited by MrAksel on Sun Mar 06, 2011 11:52 am, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

To compile files to an assembly we will use the CodeDomProvider.CompileAssemblyFromFile method.
This requires 2 parameters CompilerOptions As CompilerParameters and FileNames() As String.
We will use Options that we have set earlier in Compiler Options and some filenames that we will get with this code
Top
This requires 2 parameters CompilerOptions As CompilerParameters and FileNames() As String.
We will use Options that we have set earlier in Compiler Options and some filenames that we will get with this code
Code: Select all
This code opens a dialog for selecting files, then it tries to compile the filenames with the specified CompilerOptions and returns the result to the Results variable. Later on we can check if there were any errors with the Results.Errors property.Dim SourceFiles As New OpenFileDialog
SourceFiles.Title="Add Source Files"
SourceFiles.MultiSelect=True
Dim Results As CompilerResults
If SourceFiles.ShowDialog() = DialogResult.OK Then
Results = Provider.CompileAssemblyFromFile(Options, SourceFiles.FileNames)
End If
Top
Last edited by MrAksel on Sun Mar 06, 2011 11:52 am, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

I will now include a full example for compiling code. This will demonstrate the different properties we have been through. Also remember this project to see what you can make with the CodeDom compiler.
Top
Top
You do not have the required permissions to view the files attached to this post.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

I downloaded your example.zip
I opened it and paste in the code in my visual basic 2008, on form1, then I went to design view, I put in button1 and a textbox and openfiledialog and savefiledialog box.
there is no errors, but when I open it up to run it and click the button1 nothing happens? what do I need to put in for buttons, textboxes etc.. to make this work?
what I want to do with this, if I can figure it out is to insert a pdf, exe, and html and save it as an exe format? I want to make a form appear before an ebook is opened, can I do this using this method?
anyways, here is the code I put in form1 using your code, and I have button1 and textbox1 on the design view part of the form1.
Imports System.CodeDom.Compiler
Public Class Form1
Public Class Example
'viewtopic.php?f=38&t=5028&p=37916#p37918
Public Shared Function ExampleCompile(ByVal provider As CodeDomProvider) As Boolean
Dim Options As New CompilerParameters()
Options.GenerateExecutable = True
Options.OutputAssembly = My.Computer.FileSystem.SpecialDirectories.Desktop & "\AssemblyTest.exe"
Options.CompilerOptions = "/optimize" 'Optimize the code when we compile it
Dim OpenReferences As New OpenFileDialog
OpenReferences.Title = "Add References"
OpenReferences.Multiselect = True
If OpenReferences.ShowDialog() = DialogResult.OK Then
Options.ReferencedAssemblies.AddRange(OpenReferences.FileNames) 'Adds a range of files selected in the file dialog
End If
Options.WarningLevel = 3
Options.IncludeDebugInformation = False
Options.TreatWarningsAsErrors = False 'Do not treat warnings as errors
Options.GenerateInMemory = False 'Do not generate output in memory
Options.TempFiles = New TempFileCollection("C:\Temp", False) 'Set the temporary file collection
If provider.Supports(GeneratorSupport.EntryPointMethod) Then
Options.MainClass = "Class1"
End If
If Not provider.Supports(GeneratorSupport.Resources) Then
GoTo skipResources
End If
Dim OpenResources As New OpenFileDialog
OpenResources.Title = "Add Resources"
OpenResources.Multiselect = True
OpenResources.Filter = "Resource files(*.resource)|*.resource"
If OpenResources.ShowDialog() = DialogResult.OK Then
Options.EmbeddedResources.AddRange(OpenResources.FileNames) 'Adds a range of files selected in the file dialog
End If
skipResources:
Dim SourceFiles As New OpenFileDialog
SourceFiles.Title = "Add Source Files"
SourceFiles.Multiselect = True
'Dim files() As String = Directory.GetFiles("C:/") maybe, see http://stackoverflow.com/questions/2242 ... -as-string
Dim Files() As String = SourceFiles.FileNames 'I put this in here, = SourceFiles.FileNames
If SourceFiles.ShowDialog() = DialogResult.OK Then
Files = SourceFiles.FileNames
End If
If Files Is Nothing OrElse Files.Count <= 0 Then
MsgBox("No files selected")
Return False
End If
Dim Results As CompilerResults = provider.CompileAssemblyFromFile(Options, Files)
If Results.Errors.Count > 0 Then
Dim errors As String = ""
Dim [error] As CompilerError
For Each [error] In Results.Errors
If [error].IsWarning Then
errors &= "Warning: " & [error].ToString & vbNewLine & "File: " & [error].FileName & " Line: " & [error].Line & vbNewLine
Else
errors &= "Error: " & [error].ToString & vbNewLine & "File: " & [error].FileName & " Line: " & [error].Line & vbNewLine
End If
Next [error]
MsgBox(errors)
Return False
Else
MsgBox("Successful build")
Return True
End If
End Function
Public Shared Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim langs As String = ""
For Each lang In CodeDomProvider.GetAllCompilerInfo
For Each s As String In lang.GetLanguages
langs &= s & ", "
Next
langs &= vbNewLine
Next
Dim resultLang As String = InputBox("Select language:" & vbNewLine & langs)
Dim provider As CodeDomProvider
If CodeDomProvider.IsDefinedLanguage(resultLang) Then
provider = CodeDomProvider.CreateProvider(resultLang)
ExampleCompile(provider)
Else
MsgBox("You did not choose a valid language, maybe you miss spelled it.")
Button1_Click(Nothing, Nothing)
End If
End Sub
End Class
End Class
I hope you can help thanks
I opened it and paste in the code in my visual basic 2008, on form1, then I went to design view, I put in button1 and a textbox and openfiledialog and savefiledialog box.
there is no errors, but when I open it up to run it and click the button1 nothing happens? what do I need to put in for buttons, textboxes etc.. to make this work?
what I want to do with this, if I can figure it out is to insert a pdf, exe, and html and save it as an exe format? I want to make a form appear before an ebook is opened, can I do this using this method?
anyways, here is the code I put in form1 using your code, and I have button1 and textbox1 on the design view part of the form1.
Imports System.CodeDom.Compiler
Public Class Form1
Public Class Example
'viewtopic.php?f=38&t=5028&p=37916#p37918
Public Shared Function ExampleCompile(ByVal provider As CodeDomProvider) As Boolean
Dim Options As New CompilerParameters()
Options.GenerateExecutable = True
Options.OutputAssembly = My.Computer.FileSystem.SpecialDirectories.Desktop & "\AssemblyTest.exe"
Options.CompilerOptions = "/optimize" 'Optimize the code when we compile it
Dim OpenReferences As New OpenFileDialog
OpenReferences.Title = "Add References"
OpenReferences.Multiselect = True
If OpenReferences.ShowDialog() = DialogResult.OK Then
Options.ReferencedAssemblies.AddRange(OpenReferences.FileNames) 'Adds a range of files selected in the file dialog
End If
Options.WarningLevel = 3
Options.IncludeDebugInformation = False
Options.TreatWarningsAsErrors = False 'Do not treat warnings as errors
Options.GenerateInMemory = False 'Do not generate output in memory
Options.TempFiles = New TempFileCollection("C:\Temp", False) 'Set the temporary file collection
If provider.Supports(GeneratorSupport.EntryPointMethod) Then
Options.MainClass = "Class1"
End If
If Not provider.Supports(GeneratorSupport.Resources) Then
GoTo skipResources
End If
Dim OpenResources As New OpenFileDialog
OpenResources.Title = "Add Resources"
OpenResources.Multiselect = True
OpenResources.Filter = "Resource files(*.resource)|*.resource"
If OpenResources.ShowDialog() = DialogResult.OK Then
Options.EmbeddedResources.AddRange(OpenResources.FileNames) 'Adds a range of files selected in the file dialog
End If
skipResources:
Dim SourceFiles As New OpenFileDialog
SourceFiles.Title = "Add Source Files"
SourceFiles.Multiselect = True
'Dim files() As String = Directory.GetFiles("C:/") maybe, see http://stackoverflow.com/questions/2242 ... -as-string
Dim Files() As String = SourceFiles.FileNames 'I put this in here, = SourceFiles.FileNames
If SourceFiles.ShowDialog() = DialogResult.OK Then
Files = SourceFiles.FileNames
End If
If Files Is Nothing OrElse Files.Count <= 0 Then
MsgBox("No files selected")
Return False
End If
Dim Results As CompilerResults = provider.CompileAssemblyFromFile(Options, Files)
If Results.Errors.Count > 0 Then
Dim errors As String = ""
Dim [error] As CompilerError
For Each [error] In Results.Errors
If [error].IsWarning Then
errors &= "Warning: " & [error].ToString & vbNewLine & "File: " & [error].FileName & " Line: " & [error].Line & vbNewLine
Else
errors &= "Error: " & [error].ToString & vbNewLine & "File: " & [error].FileName & " Line: " & [error].Line & vbNewLine
End If
Next [error]
MsgBox(errors)
Return False
Else
MsgBox("Successful build")
Return True
End If
End Function
Public Shared Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim langs As String = ""
For Each lang In CodeDomProvider.GetAllCompilerInfo
For Each s As String In lang.GetLanguages
langs &= s & ", "
Next
langs &= vbNewLine
Next
Dim resultLang As String = InputBox("Select language:" & vbNewLine & langs)
Dim provider As CodeDomProvider
If CodeDomProvider.IsDefinedLanguage(resultLang) Then
provider = CodeDomProvider.CreateProvider(resultLang)
ExampleCompile(provider)
Else
MsgBox("You did not choose a valid language, maybe you miss spelled it.")
Button1_Click(Nothing, Nothing)
End If
End Sub
End Class
End Class
I hope you can help thanks
you can help him by use the code bracket things , i dont know what they're called but it makes it easier to read code
Copyright Information
Copyright © Codenstuff.com 2020 - 2023