VBCodeProvider Class

Provides access to instances of the Visual Basic code generator and code compiler.

Namespace: Microsoft.VisualBasic
Assembly: System (in system.dll)

public class VBCodeProvider : CodeDomProvider
public class VBCodeProvider extends CodeDomProvider
public class VBCodeProvider extends CodeDomProvider
Not applicable.

This class provides methods that can be used to retrieve instances of the Visual Basic ICodeGenerator and ICodeCompiler implementations.


This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands.

The following example uses either the C# or Visual Basic code provider to compile a source file. The example checks the input file extension and uses the corresponding CSharpCodeProvider or VBCodeProvider for compilation. The input file is compiled into an executable file, and any compilation errors are displayed to the console.

public static bool CompileExecutable(String sourceName)
    FileInfo sourceFile = new FileInfo(sourceName);
    CodeDomProvider provider = null;
    bool compileOk = false;

    // Select the code provider based on the input file extension.
    if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
        provider = CodeDomProvider.CreateProvider("CSharp");
    else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
        provider = CodeDomProvider.CreateProvider("VisualBasic");
        Console.WriteLine("Source file must have a .cs or .vb extension");

    if (provider != null)

        // Format the executable file name.
        // Build the output assembly path using the current directory
        // and <source>_cs.exe or <source>_vb.exe.
        String exeName = String.Format(@"{0}\{1}.exe", 
            sourceFile.Name.Replace(".", "_"));

        CompilerParameters cp = new CompilerParameters();

        // Generate an executable instead of 
        // a class library.
        cp.GenerateExecutable = true;

        // Specify the assembly file name to generate.
        cp.OutputAssembly = exeName;
        // Save the assembly as a physical file.
        cp.GenerateInMemory = false;
        // Set whether to treat all warnings as errors.
        cp.TreatWarningsAsErrors = false;
        // Invoke compilation of the source file.
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, 
        if(cr.Errors.Count > 0)
            // Display compilation errors.
            Console.WriteLine("Errors building {0} into {1}",  
                sourceName, cr.PathToAssembly);
            foreach(CompilerError ce in cr.Errors)
                Console.WriteLine("  {0}", ce.ToString());
            // Display a successful compilation message.
            Console.WriteLine("Source {0} built into {1} successfully.",
                sourceName, cr.PathToAssembly);
        // Return the results of the compilation.
        if (cr.Errors.Count > 0)
            compileOk = false;
            compileOk = true;
    return compileOk;

  • SecurityAction.LinkDemand  for full trust for the immediate caller. This class cannot be used by partially trusted code.
  • SecurityAction.InheritanceDemand  for full trust for inheritors. This class cannot be inherited by partially trusted code.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions