C# 中的运行时编译有问题吗?
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您检查
CompilerResults compres
,它表明存在异常并且编译不成功,因此它没有写出Assembly.exe
并且有一个System.IO来自
异常Process.Start()
的 .FileNotFound试试这个
If you check
CompilerResults compres
it shows that there's an exception and the compilation was not successful and hence it's not writing outAssembly.exe
and there is aSystem.IO.FileNotFound
exception fromProcess.Start()
Try this