动态创建Winforms可执行文件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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C#从项目生成动态可执行文件

来自分类Dev

在C#可执行文件中构建.sln

来自分类Dev

创建可执行文件

来自分类Dev

C ++可执行文件崩溃

来自分类Dev

“ bash可执行文件”和“ bash -c可执行文件”之间的区别

来自分类Dev

用动态参数调用可执行文件

来自分类Dev

修复可执行文件的硬编码动态链接

来自分类Dev

如何强制可执行文件链接动态库

来自分类Dev

C++ 模板的目标代码是否在可执行文件和动态库中重复?

来自分类Dev

使用Intellij创建可执行文件.jar

来自分类Dev

从Cython代码创建可执行文件

来自分类Dev

使用pyinstaller创建可执行文件时出错

来自分类Dev

如何创建包含gpiozero的可执行文件?

来自分类Dev

从Visual Studio创建可执行文件

来自分类Dev

为Linux可执行文件创建命令

来自分类Dev

如何从JAR创建Windows可执行文件

来自分类Dev

创建“可中断”可执行文件的最佳方法

来自分类Dev

创建一个可执行文件

来自分类Dev

为* nix创建Python可执行文件

来自分类Dev

尝试建立静态链接以创建可执行文件

来自分类Dev

无法使用可执行文件创建快照

来自分类Dev

如何从此源代码创建可执行文件?

来自分类Dev

创建依赖于框架的可执行文件

来自分类Dev

在包含外部库的文件夹之外运行C#可执行文件

来自分类Dev

在可执行文件中“隐藏”文件 (C++)

来自分类Dev

GCC和binutils构建-C编译器无法创建可执行文件

来自分类Dev

C编译器无法创建可执行文件

来自分类Dev

在C ++中创建可执行文件并返回int数组

来自分类Dev

配置:错误:C编译器无法创建可执行文件