Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
			}
		}
Posted

 
Share this answer
 
Comments
Ron Beyer 25-Sep-13 16:54pm    
5'd
ridoy 25-Sep-13 16:55pm    
Thank you,:).
Sergey Alexandrovich Kryukov 25-Sep-13 17:12pm    
It does not really pin-point the problem, which is actually pretty simple. Please see my answer.
—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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900