C# 动态创建winforms可执行文件#
我想创建一个应用程序,它将从textBox1中取出文本,编译它,并将其保存为可执行文件。我以前从未尝试过,但我真的很想让它工作这是我在“编译器”应用程序中使用的代码:C# 动态创建winforms可执行文件#,c#,.net,winforms,C#,.net,Winforms,我想创建一个应用程序,它将从textBox1中取出文本,编译它,并将其保存为可执行文件。我以前从未尝试过,但我真的很想让它工作这是我在“编译器”应用程序中使用的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syst
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!";
}
}
}
}
class Program
{
static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
}
}
文本框1文本,意味着我试图编译的源代码是:
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!";
}
}
}
}
class Program
{
static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
}
}
基本上,我试图动态生成一个可执行文件,它只显示一个表单。我也尝试过,而不是制作并显示一个表单来显示System.Windows.MessageBox.show(“测试”)
在这两种情况下,我都会遇到以下错误:
第5行,错误号:CS0234,'类型或命名空间名称
命名空间“System”中不存在“Windows”(是否缺少
装配参考
第5行,错误号:CS0234,'类型或命名空间名称
命名空间“System”中不存在“Windows”(是否缺少
装配参考
您正在添加3个文件(“System.dll”、“System.Windows.Forms.dll”、“System.Drawing.dll”)作为嵌入资源,而不是作为引用。将它们添加到ReferencedAssembly。您将添加3个文件(“System.dll”、“System.Windows.Forms.dll”、“System.Drawing.dll”)作为嵌入资源,而不是作为引用。改为将它们添加到ReferencedAssembly。那么您是如何解决这些错误的呢?你知道一般如何编译,以及具体工作中如何引用?你希望得到什么样的答案?显然,我希望得到一个解决问题的答案。我不知道为什么会这样。我在其中添加了System.Windows.Forms.dll。如果我使用一个更简单的源代码,只是为了制作一个控制台应用程序,它就可以工作了。那么您是如何解决这些错误的呢?你知道一般如何编译,以及具体工作中如何引用?你希望得到什么样的答案?显然,我希望得到一个解决问题的答案。我不知道为什么会这样。我在其中添加了System.Windows.Forms.dll。如果我使用一个更简单的源代码,只是为了制作一个控制台应用程序,它就可以工作了。呵呵,谢谢你,伙计。有效:)我会在8分钟内接受答案。呵呵,谢谢你,伙计。我会在8分钟内接受答案。