Skip to main content

Create .exe file from C# code

Wow! What a gr88 feature in .Net… Now you can create your own executable file directly on the runtime. Just supply the source code and create an .exe as you want it to be.

Here is the code snippet; I was surprised to see how easy it was.

Then just place this code in a Button Click event.

Microsoft.CSharp.CSharpCodeProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cpar = new System.CodeDom.Compiler.CompilerParameters();
string pathIcon = Application.StartupPath + "\\icon.ico";
cpar.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" + pathIcon + "\"";
cpar.IncludeDebugInformation = false;
cpar.TreatWarningsAsErrors = false;
cpar.OutputAssembly = Application.StartupPath + "\\MyApplication.exe";
cpar.GenerateInMemory = false;
cpar.GenerateExecutable = true;
cpar.ReferencedAssemblies.Add("System.dll");
cpar.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cpar.ReferencedAssemblies.Add("System.Drawing.dll");

//List all dll's to which your program would refer to.
string src = //C-Sharp source code (Or you can directly give the path of .cs file)
System.CodeDom.Compiler.CompilerResults cr = ic.CompileAssemblyFromSource(cpar, src.Trim());
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) MessageBox.Show(ce.ErrorText);


Here in the code, you can see that I have specified a path for an icon of my application “icon.ico”. You can just change the path and file name with your own file.
Also, same is with the application name “MyApplication.exe”; give what ever name you like :-)

That’s all! This way you can create your own application… Enjoy…
And yea, please do not forget to leave a comment on this post.

Comments

Popular posts from this blog

Hiding / Disabling Maximize and/or Minimize Box in WPF

After trying my hands on the WPF, I realized that I could convert my existing Windows Forms applications into rich looking WPF applications. The task of migration was definitely not easy. During the course of migration, I came across many hurdles and seemed it was impossible to achieve in WPF. But thanks to the msdn help, which made a lot of things easier :-) Since I was converting my existing application, I started looking for the similar features in WPF. The problem I faced was in the initial phase itself where I wanted to disable MaximizeBox button in WPF. This posed a challenge to me since I had no clue as to how to achieve this. After searching the net for a while, I got a solution which I will share with you all to make your life easier. Disabling Maximize Box Button on the WPF Window: This can be done as follows: Put this piece of code above the main class. internal static class WindowExtensions {      [DllImport("user32.dll")]      internal exte...

Add, Delete and Move controls at runtime.

In this article you will learn how to: Add or delete controls at runtime. Access controls with the indexer. Create control collection using Hash Table. Download full source code. To start with, create a windows application project and add a class to it. We will create a button array so name it as “ButtonArray.cs”. Add the following code to it: Class “ButtonArray” is inherited from the Hash table, so it will be easy for us to add buttons in the collection. Let’s have a look into the “AddButton” method. In this method, we create an instance of a button with the necessary properties and add it to the hash table as well as to the form’s control collection. Each time the control is added in the hash table with its unique key. This helps us to access the controls when required. Now we’ll see how to remove control from the hash table and form’s control collection. In order to remove the control, just pass the key. That’s simple! Isn’t it? :-) Ok! That w...

Create a Virtual Drive through a Command Prompt

Just a few weeks back, I was working on a very old application where in all the directory paths used in the code were hard coded and pointing to a S:/ drive. Since I did not have a drive "S" on my machine, and I had to make the application up and running, I was left two choices: 1. Rename all the directory paths in the application, OR 2. Create a Virtual Drive Option 1 seemed to be a lot time consuming and hence I chose option 2 which I felt was a lot easier to do. I searched on the net, and found out one command which could be used to create virtual drives. Windows OS has an application called " subst"  which helps in creating virtual drives mapped to the local folder. The syntax of the command is: SUBST [drive1: [drive2:]path] Example: subst S: C:\Dev When you run this command,  a drive will be created with a letter S: assigned to a local folder "Dev" on drive C.  This is an easy way to create Virtual Drives, though the drives...