using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.CodeDom; using System.CodeDom.Compiler; using Microsoft.CSharp; using System.Diagnostics; namespace DotNetGuruCodeEditor { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button2; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Button compiler; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// /// private void InitializeComponent() { this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.compiler = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // textBox2 // this.textBox2.BackColor = System.Drawing.SystemColors.Control; this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.textBox2.ForeColor = System.Drawing.SystemColors.WindowText; this.textBox2.Location = new System.Drawing.Point(24, 280); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(320, 88); this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 24); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(320, 184); this.textBox1.TabIndex = 0; this.textBox1.Text = "textBox1"; // // compiler // this.compiler.Location = new System.Drawing.Point(48, 408); this.compiler.Name = "compiler"; this.compiler.Size = new System.Drawing.Size(88, 27); this.compiler.TabIndex = 1; this.compiler.Text = "Compiler"; this.compiler.Click += new System.EventHandler(this.compiler_Click); // // button2 // this.button2.Location = new System.Drawing.Point(192, 408); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(88, 27); this.button2.TabIndex = 1; this.button2.Text = "Exécuter"; this.button2.Click += new System.EventHandler(this.compiler_Click); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1}); this.groupBox1.Location = new System.Drawing.Point(8, 16); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(344, 232); this.groupBox1.TabIndex = 3; this.groupBox1.TabStop = false; this.groupBox1.Text = "Programme Source"; // // groupBox2 // this.groupBox2.Location = new System.Drawing.Point(8, 264); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(344, 120); this.groupBox2.TabIndex = 4; this.groupBox2.TabStop = false; this.groupBox2.Text = "Messages Compilateur"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(360, 460); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button2, this.textBox2, this.compiler, this.groupBox1, this.groupBox2}); this.Name = "Form1"; this.Text = "DotNetGuru C# Editor"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void compiler_Click(object sender, System.EventArgs e) { // Utilisation de l'API pour demander un provier C# // Pattern Factory CSharpCodeProvider codeProvider = new CSharpCodeProvider(); ICodeCompiler icc = codeProvider.CreateCompiler(); // Nom temporaire du programme qu'on veut générer (hardcodé) // Peut être paramétré dans l'IHM string Output = "tmp.exe"; Button ButtonObject = (Button) sender; textBox2.Text = ""; System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); // Nous utiliserons Process.Start() pour lancer l'application ainsi créée // C'est pourquoi, il est préférable de générer un EXE parameters.GenerateExecutable = true; parameters.OutputAssembly = Output; // Lance la compilation et récupère les messages du compilateur C# CompilerResults results = icc.CompileAssemblyFromSource(parameters,textBox1.Text); // On itère sur l'ensemble des messages d'erreurs 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 { //La compilation s'est bien passée textBox2.ForeColor = Color.Blue; textBox2.Text = "Compilation OK"; //Si l'utilisateur l'a demandé, on exécute dans un process séparé le programme if (ButtonObject.Text == "Run") Process.Start(Output); } } } }