-
C# code
-
// 源代码字符串(最好从文件中读取) string source = " using System; " + " using System.Windows.Forms; " + " namespace App2 " + " { " + " public class MainForm : Form " + " { " + " static void Main(string[] args) " + " { " + " " + " } " + " } " + " } " ; // 初始化C#编译器 Microsoft.CSharp.CSharpCodeProvider CScodeProvider = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler icodeCompiler = CScodeProvider.CreateCompiler(); // 设置C#编译器编译参数 System.CodeDom.Compiler.CompilerParameters compilerParameters = new System.CodeDom.Compiler.CompilerParameters(); compilerParameters.GenerateInMemory = false ; // 生成文件 compilerParameters.GenerateExecutable = true ; // 生成可执行文件 compilerParameters.ReferencedAssemblies.Add( " System.dll " ); // 添加程序集引用 compilerParameters.ReferencedAssemblies.Add( " System.Windows.Forms.dll " ); compilerParameters.ReferencedAssemblies.Add( " System.Drawing.dll " ); compilerParameters.OutputAssembly = Application.StartupPath + " \\2.exe " ; // 指定输出文件 // 开始使用这个参数,对脚本代码进行编译 System.CodeDom.Compiler.CompilerResults compilerResults = icodeCompiler.CompileAssemblyFromSource(compilerParameters, source); // 如果编译出错,那么出错信息就在compilerResults变量内