通过c#程序生成.exe文件 [英] Generate .exe files by a c# program
问题描述
有人可以帮助我知道如何从ac#program生成一个.exe文件吗?
i尝试了这些代码,但它没有成功
AppDomain ad = AppDomain.CurrentDomain;
System.Reflection.AssemblyName an = new System.Reflection.AssemblyName();
an.Name = Childprogram;
System.Reflection.Emit.AssemblyBuilder ab = ad.DefineDynamicAssembly(an,System.Reflection.Emit.AssemblyBuilderAccess.Save);
System.Reflection.Emit.ModuleBuilder mb = ab.DefineDynamicModule( testmodule,< span class =code-string> Childprogram.exe);
System.Reflection.Emit.TypeBuilder tb = mb.DefineType( testtype,System .Reflection.TypeAttributes.Public);
System.Reflection.Emit.MethodBuilder metb = tb.DefineMethod( hi,System .Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static, null , null );
// mb.SetUserEntryPoint(metb);
ab.SetEntryPoint(metb );
System.Reflection.Emit.ILGenerator ilg = metb.GetILGenerator();
ilg.EmitWriteLine( hiii worrld);
ilg.Emit(System.Reflection.Emit.OpCodes.Ret);
tb.IsCreated();
ab.Save( Childprogram.exe,System.Reflection.PortableExecutableKinds.ILOnly, System.Reflection.ImageFileMachine.IA64);
但它给出了一个错误:
类型'testtype'未完成。
\
有什么问题?
i也试过这些代码:
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters();
cp.ReferencedAssemblies.Add( System.dll);
cp.GenerateInMemory = false ;
cp.GenerateExecutable = true ;
cp.OutputAssembly = Childprogram.exe;
System.CodeDom.Compiler.CompilerResults cr = cscp.CreateCompiler()。CompileAssemblyFromFile(cp, @ a的.cs跨度>);
if (cr.Errors.Count > 0 )MessageBox.Show(cr.Errors.Count.ToString()+ cr.Errors [ 0 ]。ToString()+ cr.Errors [ 0 ]。FileName);
但这个给我错误这就是说我在Childprogram.exe中有一个静态主方法!
这是我的源文件代码:
使用系统;
命名空间 RDPCracker
{
static class 计划
{
静态 void Main ()
{
Console.WriteLine( asd);
}
}
}
有什么问题?
非常感谢花时间给我!
Quote:但是这个给我错误就是说我在Childprogram.exe中有一个静态主方法!
它有正确!
在这里查看hello world应用程序: http://msdn.microsoft.com /en-us/library/aa288463(v=vs.71).aspx [ ^ ]
看到差异?
您的类和主方法是静态的,但不是公共!
hey !
can someone help me to know how generate a .exe file from a c# program?
i tried these codes , but its not succesfull
AppDomain ad = AppDomain.CurrentDomain;
System.Reflection.AssemblyName an = new System.Reflection.AssemblyName();
an.Name = "Childprogram";
System.Reflection.Emit.AssemblyBuilder ab = ad.DefineDynamicAssembly(an, System.Reflection.Emit.AssemblyBuilderAccess.Save);
System.Reflection.Emit.ModuleBuilder mb = ab.DefineDynamicModule("testmodule", "Childprogram.exe");
System.Reflection.Emit.TypeBuilder tb = mb.DefineType("testtype", System.Reflection.TypeAttributes.Public);
System.Reflection.Emit.MethodBuilder metb = tb.DefineMethod("hi", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static,null, null);
//mb.SetUserEntryPoint(metb);
ab.SetEntryPoint(metb);
System.Reflection.Emit.ILGenerator ilg = metb.GetILGenerator();
ilg.EmitWriteLine("hiii worrld");
ilg.Emit(System.Reflection.Emit.OpCodes.Ret);
tb.IsCreated();
ab.Save("Childprogram.exe",System.Reflection.PortableExecutableKinds.ILOnly,System.Reflection.ImageFileMachine.IA64);
but it gives an error :
Type 'testtype' was not completed.
\
what is problem?
i also tried these codes :
Microsoft.CSharp.CSharpCodeProvider cscp = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.GenerateInMemory = false;
cp.GenerateExecutable = true;
cp.OutputAssembly = "Childprogram.exe";
System.CodeDom.Compiler.CompilerResults cr= cscp.CreateCompiler().CompileAssemblyFromFile(cp, @"a.cs");
if (cr.Errors.Count > 0) MessageBox.Show(cr.Errors.Count.ToString()+cr.Errors[0].ToString()+cr.Errors[0].FileName);
but this one give me error to that is saying i dnt have a static main method in Childprogram.exe !
wheras this is my sourcefile code :
using System;
namespace RDPCracker
{
static class Program
{
static void Main()
{
Console.WriteLine("asd");
}
}
}
what is problem?
very thanks to spend time for me!
Quote:but this one give me error to that is saying i dnt have a static main method in Childprogram.exe !
And it has right!
Look at the hello world app here: http://msdn.microsoft.com/en-us/library/aa288463(v=vs.71).aspx[^]
See the difference?
Your class and main method is static, but not public!
这篇关于通过c#程序生成.exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!