How to programmatically compile code using C# compiler
How to programmatically compile code using C# compiler
- Create a new Visual C# .NET Windows application. Form1 is created by default.
- Add a Button control to Form1, and then change its Text property to Build.
- Add another Button control to Form1, and then change its Text property to Run.
- Add two TextBox controls to Form1, set the Multiline property for both controls to True, and then size these controls so that you can paste multiple lines of text into each one.
- In the code editor, open the Form1.cs source file.
- In the Form1 class, Write button click handler(In Ex Pgm)
- In Form1.cs, locate the Form1 constructor.
- After the call to InitializeComponent in the Form1 constructor, add the following code to wire the button click handler to both buttons that you added to Form1.
- Run the project. After Form1 loads, click the Build button. Notice that you get a compiler error.
- Next, copy the following text into the textbox, replacing any existing text:
using System; namespace HelloWorld { /// <summary> /// Summary description for Class1. /// </summary> class HelloWorldClass { static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadLine(); } } }
- Click Build again. The compile should be successful.
- Click Run, and it will compile the code and run the resulting executable file. The compile creates a executable file called "Out.exe", which is saved in the same folder as the application that you are running.
NOTE: You can modify the code in the textbox to see different compiler errors. For example, delete one of the semi-colons and rebuild the code. - Lastly, modify the code in the textbox to output another line of text to the console window. Click Run to see the output.
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
namespace SpikeForWF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Click += new System.EventHandler(this.button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "Out.exe";
Button ButtonObject = (Button)sender;
textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);
if (results.Errors.Count > 0)
{
textBox2.ForeColor = Color.Red;
foreach (CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
//If we clicked run then launch our EXE
if (ButtonObject.Text == "Run") Process.Start(Output);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Comments
Post a Comment