Click here to Skip to main content
11,701,590 members (69,568 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# execution reflection
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);
            System.Reflection.Emit.ILGenerator ilg = metb.GetILGenerator();
            ilg.EmitWriteLine("hiii worrld");

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.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()

what is problem?
very thanks to spend time for me!
Posted 20-Apr-13 7:07am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^]
See the difference?
Your class and main method is static, but not public!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 324
1 Maciej Los 275
2 Sergey Alexandrovich Kryukov 250
3 Mika Wendelius 189
4 CHill60 110
0 OriginalGriff 8,274
1 Sergey Alexandrovich Kryukov 7,317
2 CPallini 4,912
3 Maciej Los 4,146
4 Mika Wendelius 3,556

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100