C# 中的运行时编译有问题吗?

发布于 2024-11-14 19:38:59 字数 1733 浏览 0 评论 0原文

我想从 http://pastehtml.com/view/awono3xoq.txt 下载代码,将其保存为字符串,然后编译&单击按钮时运行它,但我似乎无法让下面的代码工作:

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.IO;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices; 

namespace ASV
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void compile()
    {
        CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
        ICodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();
        String [] referenceAssemblies = {"System.dll"};
        string myAssemblyName = "Assembly.exe";
        CompilerParameters myCompilerParameters = new CompilerParameters(referenceAssemblies, myAssemblyName);
        myCompilerParameters.GenerateExecutable = true;
        myCompilerParameters.GenerateInMemory = true;
        WebClient x = new WebClient();
        Stream y = x.OpenRead("http://pastehtml.com/view/awono3xoq.txt");
        StreamReader z = new StreamReader(y);
        string source = z.ReadToEnd();
        z.Close();
        y.Close();
        CompilerResults compres = myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, source);
        Process.Start("Assembly.exe");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        compile();
    }
}
}

我做错了什么(除了有太多的 using 语句:P)?

I want to download the code from http://pastehtml.com/view/awono3xoq.txt, save it to a string, then compile & run it when the button is clicked, but I can't seem to get the code below to work:

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.IO;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices; 

namespace ASV
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public void compile()
    {
        CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
        ICodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();
        String [] referenceAssemblies = {"System.dll"};
        string myAssemblyName = "Assembly.exe";
        CompilerParameters myCompilerParameters = new CompilerParameters(referenceAssemblies, myAssemblyName);
        myCompilerParameters.GenerateExecutable = true;
        myCompilerParameters.GenerateInMemory = true;
        WebClient x = new WebClient();
        Stream y = x.OpenRead("http://pastehtml.com/view/awono3xoq.txt");
        StreamReader z = new StreamReader(y);
        string source = z.ReadToEnd();
        z.Close();
        y.Close();
        CompilerResults compres = myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, source);
        Process.Start("Assembly.exe");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        compile();
    }
}
}

what am I doing wrong (other than having too many using statments :P)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-11-21 19:38:59

如果您检查CompilerResults compres,它表明存在异常并且编译不成功,因此它没有写出Assembly.exe并且有一个System.IO来自 Process.Start() 的 .FileNotFound 异常

试试这个

public void compile()
{
    CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
    ICodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();
    string myAssemblyName = @"Assembly.exe";
    CompilerParameters myCompilerParameters = new CompilerParameters
                                                  {
                                                      OutputAssembly = myAssemblyName,
                                                      GenerateExecutable = true,
                                                      GenerateInMemory = true
                                                  };
    myCompilerParameters.ReferencedAssemblies.Add("System.dll");
    WebClient x = new WebClient();
    Stream y = x.OpenRead("http://pastehtml.com/view/awono3xoq.txt");
    StreamReader z = new StreamReader(y);
    string source = z.ReadToEnd();
    z.Close();
    y.Close();
    CompilerResults compres = myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, source);
    Process.Start(myAssemblyName);
}

If you check CompilerResults compres it shows that there's an exception and the compilation was not successful and hence it's not writing out Assembly.exe and there is a System.IO.FileNotFound exception from Process.Start()

Try this

public void compile()
{
    CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
    ICodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();
    string myAssemblyName = @"Assembly.exe";
    CompilerParameters myCompilerParameters = new CompilerParameters
                                                  {
                                                      OutputAssembly = myAssemblyName,
                                                      GenerateExecutable = true,
                                                      GenerateInMemory = true
                                                  };
    myCompilerParameters.ReferencedAssemblies.Add("System.dll");
    WebClient x = new WebClient();
    Stream y = x.OpenRead("http://pastehtml.com/view/awono3xoq.txt");
    StreamReader z = new StreamReader(y);
    string source = z.ReadToEnd();
    z.Close();
    y.Close();
    CompilerResults compres = myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, source);
    Process.Start(myAssemblyName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文