Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

CSharpCodeProvider Class

Provides access to instances of the C# code generator and code compiler.

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

public class CSharpCodeProvider : CodeDomProvider
public class CSharpCodeProvider extends CodeDomProvider
public class CSharpCodeProvider extends CodeDomProvider

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

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 = new Microsoft.CSharp.CSharpCodeProvider();
    else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
        provider = new Microsoft.VisualBasic.VBCodeProvider();
        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;

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft