[英]Creating winforms executable dynamically C#
我想創建一個應用程序,它將從textBox1中取出文本,進行編譯,然后將其另存為可執行文件。 我以前從未嘗試過,但是我真的很想讓它工作。 這是我在“編譯器”應用程序中使用的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Diagnostics;
namespace Compiler
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "out.exe";
Button ButtonObject = (Button)sender;
CompilerParameters parameters = new CompilerParameters();
string[] references = { "System.dll","System.Windows.Forms.dll","System.Drawing.dll" };
parameters.EmbeddedResources.AddRange(references);
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
MessageBox.Show(CompErr.ToString());
}
}
else
{
//Successful Compile
textBox1.ForeColor = Color.Blue;
textBox1.Text = "Success!";
}
}
}
}
textbox1文本,表示我要編譯的源是:
class Program
{
static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
}
}
基本上,我試圖動態生成一個可執行文件,該文件僅顯示一個表單。 我也嘗試過,而不是制作並顯示一個表單來顯示System.Windows.MessageBox.Show(“ testing”);。
在兩種情況下,我都會收到此錯誤:
行號5,錯誤號:CS0234,類型或名稱空間名稱“ Windows”在名稱空間“系統”中不存在(您是否缺少程序集引用?);
行號5,錯誤號:CS0234,類型或名稱空間名稱“ Windows”在名稱空間“系統”中不存在(您是否缺少程序集引用?);
您正在添加3個文件(“ System.dll”,“ System.Windows.Forms.dll”,“ System.Drawing.dll”)作為嵌入式資源,而不是作為引用。 而是將它們添加到ReferencedAssemblies。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.