帮助在运行时创建exe [英] help for create exe on run time
本文介绍了帮助在运行时创建exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码但是创建了dll文件,如何转换为exe文件
i have this code but create dll file, how to convert to exe file
private void btnCompile_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider csp = new CSharpCodeProvider();
ICodeCompiler cc = csp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.OutputAssembly = Application.StartupPath + "\\TestClass.dll";
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("CSharpScripter.exe");
cp.WarningLevel = 3;
cp.CompilerOptions = "/target:library /optimize";
cp.GenerateExecutable = false;
cp.GenerateInMemory = false;
System.CodeDom.Compiler.TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false);
CompilerResults cr = new CompilerResults(tfc);
cr = cc.CompileAssemblyFromSource(cp, this.rtfCode.Text);
if (cr.Errors.Count > 0)
{
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText);
}
MessageBox.Show(this, "Errors occoured", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.btnExecute.Enabled = false;
}
else
{
this.btnExecute.Enabled = true;
}
System.Collections.Specialized.StringCollection sc = cr.Output;
foreach (string s in sc)
{
Console.WriteLine(s);
}
}
推荐答案
您需要查看:
CompilerParameters.GenerateExecutable Property [ ^ ]
并且还:
在运行时制作可执行文件 [ ^ ]
从源代码动态创建编译的.NET exe [ ^ ]
You need to have a look at:
CompilerParameters.GenerateExecutable Property [^]
And also:
Make an executable at runtime[^]
Dynamically create a compiled .NET exe from source code[^]
您无需创建PE文件。编译成功后,始终会创建它。也许您可能只需要从临时位置复制它。它在哪里?这里:
http:// msdn。 microsoft.com/en-us/library/system.codedom.compiler.compilerresults.pathtoassembly.aspx [ ^ ]。
一些背景:它实际上很简单检查现有的CodeDOM提供程序是否基于与(自由重新分发的).NET Framework捆绑在一起的编译器:VB.NET和C#。这些编译器实际上没有CodeDOM的概念(这就是CodeDOM功能目前受限的原因:如果您尝试从源代码获取DOM树,您将获得不支持消息)。但是这个原因,编译总是从源文件中创建PE文件,没有别的,没有编译到内存或解析到DOM树。相反,这些提供程序的CodeDOM只是作为编译器的包装器实现。当你需要编译到内存中加载的程序集时,你实际上得到一个PE文件,然后以通常的方式从它加载程序集。所以,如果编译成功,你总会在输出上有一些PE文件。
-SA
You don't need to "create" a PE file. It is always created as soon as your compilation is successful. Perhaps you may only need to copy it from the temporary location. Where is it? Here:
http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerresults.pathtoassembly.aspx[^].
Some background: it's actually easy to check up that existing CodeDOM providers are based on the compilers which are bundled with the (freely redistributed) .NET Framework: VB.NET and C#. These compilers actually have no notion of CodeDOM (and that's why CodeDOM functionality is currently limited: if you try to get a DOM tree from source, you will get a "not supported" messages). But this reason, compilation always make PE files from source files, nothing else, there is no a compilation into memory or parsing to the DOM tree. Instead, CodeDOM for these providers is simply implementing as the wrappers over the compilers. When you need a compilation into an assembly loaded in memory, you actually get a PE file, and then the assembly is loaded from it in a usual way. So, you always have some PE file on output in case of successful compilation.
—SA
这篇关于帮助在运行时创建exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文