Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[TuT] Use CodeDom to compile a C# source

You want to start learning C#?
You want to know how to compile a file (coded in C#) with your VB.NET program?
Just take a look at this.

First, add a textbox to your form, call it "Compile!" or what you want.
Then add those lines:

Code
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Public Class Form1
    Private Sub Compile(ByVal InputSource As String, ByVal OutputPE As String)
        Dim icc As CodeDomProvider = New CSharpCodeProvider()
        Dim parameters As New CompilerParameters()
        parameters.GenerateExecutable = True
        parameters.OutputAssembly = OutputPE
        'Compiling parameters
        parameters.ReferencedAssemblies.Add("System.dll")
        parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        parameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
        parameters.ReferencedAssemblies.Add("System.Data.dll")
        parameters.ReferencedAssemblies.Add("System.Deployment.dll")
        parameters.ReferencedAssemblies.Add("System.Drawing.dll")
        parameters.ReferencedAssemblies.Add("System.Xml.dll")
        parameters.CompilerOptions = "/filealign:0x00000200 /optimize+ /platform:anycpu /debug- /target:winexe"
        Dim results As CompilerResults = icc.CompileAssemblyFromSource(parameters, InputSource)
        'Check if any errors
        If results.Errors.Count > 0 Then
            For Each CompErr As CompilerError In results.Errors
                MessageBox.Show((("Line number " & CompErr.Line & ", Error Number: ") + CompErr.ErrorNumber & ", '") + CompErr.ErrorText & ";")
            Next
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Compile(My.Resources.String1, "MyFuckingName.exe")
    End Sub
End Class

Ok, string1 doesn't exist. So now, go to the properties of your project and click on "resources" then add those lines to the value of string1:

Code
using System.IO;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Diagnostics;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {
MessageBox.Show ("Your program is working!", "¶_¶ rocks",
                       MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            //
            // Your Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion
                static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

}
}

THAT'S ALL!
Generate the .EXE of your project, open it & click on your button. The C# thing is compiled.
Don't forget to say thanks!
This is amazing work it will make compiling stubs in codeDom much easier thank you I would rep++ you if I was leet
No problem. =)

I wanna edit my thread to add some cool functions.
and vb.net
can you make one for vb
nice man, thanks for the share! I might be using thisss
I <3 CodeDom. A fun thing you can do, is host your code somewhere like PasteBin, or Pastie, and compile it from there.
Looks good broski! +Rep!

/short
This is really great. Thanks a lot.

Please help me with this. I wanna add a button to the form and display a messagebox when clicked.

I tried with the following code. But, I get no button on the form

Code
using System.IO;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
  public Form1()
  {
    InitializeComponent();
  }

        private void Form1_Load(object sender, EventArgs e)
  {
        MessageBox.Show ("Welcome to Stupid world", "Jaikrishna rocks",
     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void button1_Click(object sender, EventArgs e)
  {
    MessageBox.Show("Are you still staying here :D");
  }

  #region Windows Form Designer generated code

  private void InitializeComponent()
  {
            //
    // Your Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
            //button
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(51, 50);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);

  }
  #endregion

    private System.Windows.Forms.Button button1;

  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }

    }
}
(Jun 21, 2010 01:47 PM)Vorfin Wrote:  I <3 CodeDom. A fun thing you can do, is host your code somewhere like PasteBin, or Pastie, and compile it from there.

Didn't know it. That's great! :D

Thanks Death' for your +REP. ^^


(Jun 21, 2010 07:02 PM)jaikrishna Wrote:  This is really great. Thanks a lot.

Please help me with this. I wanna add a button to the form and display a messagebox when clicked.

I tried with the following code. But, I get no button on the form

Code
using System.IO;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
  public Form1()
  {
    InitializeComponent();
  }

        private void Form1_Load(object sender, EventArgs e)
  {
        MessageBox.Show ("Welcome to Stupid world", "Jaikrishna rocks",
  MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void button1_Click(object sender, EventArgs e)
  {
    MessageBox.Show("Are you still staying here :D");
  }

  #region Windows Form Designer generated code

  private void InitializeComponent()
  {
            //
    // Your Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
            //button
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(51, 50);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);

  }
  #endregion

    private System.Windows.Forms.Button button1;

  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }

    }
}

Try using this:
Code
public class ButtonclsForm : System.Windows.Forms.Form
    {
  private Button button1;
  public ButtonclsForm()
  {
    Text = "A Simple Form";
    button1 = new Button ();
//    SuspendLayout();
    button1.Text = "Cancel";
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size (72, 30);
    button1.Location = new System.Drawing.Point ((ClientRectangle.Width - button1.Size.Width) / 2, ClientRectangle.Height - 35);
    Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
    button1.Click += new System.EventHandler(OnClickButton1);
//    ResumeLayout (false);
  }
  static public void Main()
  {
    Application.Run(new ButtonclsForm());
  }
  void OnClickButton1 (object sender, System.EventArgs e)
  {
    Application.Exit ();
  }
    }

And replace Application.Exit (); with what you want.
Try using this:
Code
public class ButtonclsForm : System.Windows.Forms.Form
    {
  private Button button1;
  public ButtonclsForm()
  {
    Text = "A Simple Form";
    button1 = new Button ();
//    SuspendLayout();
    button1.Text = "Cancel";
    button1.Name = "button1";
    button1.Size = new System.Drawing.Size (72, 30);
    button1.Location = new System.Drawing.Point ((ClientRectangle.Width - button1.Size.Width) / 2, ClientRectangle.Height - 35);
    Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
    button1.Click += new System.EventHandler(OnClickButton1);
//    ResumeLayout (false);
  }
  static public void Main()
  {
    Application.Run(new ButtonclsForm());
  }
  void OnClickButton1 (object sender, System.EventArgs e)
  {
    Application.Exit ();
  }
    }

And replace Application.Exit (); with what you want.
[/quote]
Thanks a lot. That was great🤗

But, Can you please explain why the previous code didn't work.
You are giving the same code here, but directly in Public Form() which I gave in InitializeComponent() and called it.
I dunno why but InitializeComponent() makes the app bugging.
I'm not really good at C#. But I do can basic things like the code I give you.
B U M P !!!

Spoiler (Click to View)
thanks a lot mate,i will try this,do you have some more things which could help me in learning c#
No problem protocol™.
Use google or this forums.
Would you mind showing the code to compile a vb.net source? instead of compile a c# source..

[Image: 2f4032b5f1170bc8fedd7e367c680327.png]



Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by WubixMar 24, 2018 06:47 PM 5 242 Mar 24, 2018 06:47 PM
Last Post: Wubix
[.NET] by HackyerJul 8, 2016 06:24 PM 2 148 Jul 8, 2016 06:24 PM
Last Post: Hackyer
[.NET] by KrypteXMay 11, 2016 03:52 PM 25 2,043 May 11, 2016 03:52 PM
Last Post: KrypteX