[TuT] Use CodeDom to compile a C# source - ¶_¶ - Jun 21, 2010
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!
* - pr0t0typ3 - Jun 21, 2010
This is amazing work it will make compiling stubs in codeDom much easier thank you I would rep++ you if I was leet
* - ¶_¶ - Jun 21, 2010
No problem. =)
I wanna edit my thread to add some cool functions.
* - no1me - Jun 21, 2010
and vb.net
can you make one for vb
* - Modesty - Jun 21, 2010
nice man, thanks for the share! I might be using thisss
* - Vorfin - Jun 21, 2010
I <3 CodeDom. A fun thing you can do, is host your code somewhere like PasteBin, or Pastie, and compile it from there.
* - Deathader - Jun 21, 2010
Looks good broski! +Rep!
/short
* - jaikrishna - Jun 21, 2010
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 22, 2010
(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.
* - jaikrishna - Jun 22, 2010
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.
* - ¶_¶ - Jun 24, 2010
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.
* - ¶_¶ - Jun 30, 2010
B U M P !!!
too fucking short
* - protocol™ - Jun 30, 2010
thanks a lot mate,i will try this,do you have some more things which could help me in learning c#
* - ¶_¶ - Jul 4, 2010
No problem protocol™.
Use google or this forums.
* - Alison Wonderland - Nov 19, 2010
Would you mind showing the code to compile a vb.net source? instead of compile a c# source..
|