新建一个应用程序项目
引用:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Resources;
using System.Drawing;
在窗体上建2个richTextBox:
1、richTextBoxCode --粘贴待编译的源代码
2、richTextBox1 --输出信息
再建一个Button并添加事件:
private void button1_Click(object sender, EventArgs e)
{
Compile();
}
//编译方法过程
private void Compile(){
// CSharpCodePrivoder
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
CompilerParameters objCompilerParameters = new CompilerParameters();
string resourceFile = "MyResources.resources";
CreateResources(resourceFile);//创建资源文件
//objCompilerParameters.EmbeddedResources.Add(resourceFile); // 添加内置资源
objCompilerParameters.CompilerOptions = " /optimize";
objCompilerParameters.CompilerOptions += " /target:winexe";
//objCompilerParameters.CompilerOptions += " /win32res:4.jpg";
//objCompilerParameters.CompilerOptions += " /res:" + resourceFile;
objCompilerParameters.CompilerOptions += " /win32icon:Setup.ico"; //添加编译图标文件
objCompilerParameters.MainClass = "DCoder.Program";
objCompilerParameters.GenerateExecutable = true;
objCompilerParameters.GenerateInMemory = false;
objCompilerParameters.TreatWarningsAsErrors = false;
objCompilerParameters.IncludeDebugInformation= false;
objCompilerParameters.OutputAssembly = "wgscd.exe";
//引用dll
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Drawing.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
// 添加内置资源
objCompilerParameters.EmbeddedResources.Add("1.jpg");
objCompilerParameters.EmbeddedResources.Add("2.jpg");
objCompilerParameters.EmbeddedResources.Add("3.jpg");
string[] sourceCode = new string[] { richTextBoxCode.Text };
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(objCompilerParameters, sourceCode);
if (cr.Errors.HasErrors)
{
display("编译错误:");
foreach (CompilerError err in cr.Errors)
{
display(err.ErrorText);
}
}
else
{
//// 通过反射,调用HelloWorld的实例
//Assembly objAssembly = cr.CompiledAssembly;
//object objHelloWorld = objAssembly.CreateInstance("wgscd.HelloWorld");
//MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
//// 调用执行
//objMI.Invoke(objHelloWorld, null);
}
}
private void display(string strInput) {
richTextBox1.AppendText(strInput+Environment.NewLine);
}
private void CreateResources(string resourceFile)
{
ResourceWriter rw = new ResourceWriter(resourceFile);
Image img = Image.FromFile("2.jpg");
rw.AddResource("myimg", img);
rw.Close();
// Assembly asbl = GetType().Assembly;
////read resources
//Assembly asbl = Assembly.GetExecutingAssembly();
//ResourceManager rm = ResourceManager.CreateFileBasedResourceManager("demo", ".", null);
//pictureBox1.Image = (Image)rm.GetObject("myimg");
}
下面是全部要测试编译的代码:复制下面全部代码到那个richTextBoxCode里再点那个窗体上的Button进行编译
//---------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.Reflection;
namespace DCoder
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// MessageBox.Show("you click me");
//显示里面的资源文件名
foreach (string str in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
display(str);
}
//读取资源文件里的一张图片显示在图片控件上
pictureBox1.Image = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("1.jpg"));
}
private void display(string strInput)
{
richTextBox1.AppendText(strInput + Environment.NewLine);
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Form1_Load");
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.richTextBoxCode = new System.Windows.Forms.RichTextBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(39, 321);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(518, 87);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(394, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 45);
this.button1.TabIndex = 2;
this.button1.Text = "Compile";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(29, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(263, 46);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// richTextBoxCode
//
this.richTextBoxCode.Location = new System.Drawing.Point(12, 78);
this.richTextBoxCode.Name = "richTextBoxCode";
this.richTextBoxCode.Size = new System.Drawing.Size(565, 206);
this.richTextBoxCode.TabIndex = 4;
this.richTextBoxCode.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(589, 420);
this.Controls.Add(this.richTextBoxCode);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.RichTextBox richTextBoxCode;
}
//----------main----------------
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}