Reputation: 279
I want to create an application, that will take out the text from textBox1, compile it, and save it as an executable. I never tried this before, but I would really like to get it working. This is the code that I'm using in my "compiler" application:
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 Microsoft.CSharp;
using System.Diagnostics;
namespace Compiler
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "out.exe";
Button ButtonObject = (Button)sender;
CompilerParameters parameters = new CompilerParameters();
string[] references = { "System.dll","System.Windows.Forms.dll","System.Drawing.dll" };
parameters.EmbeddedResources.AddRange(references);
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
MessageBox.Show(CompErr.ToString());
}
}
else
{
//Successful Compile
textBox1.ForeColor = Color.Blue;
textBox1.Text = "Success!";
}
}
}
}
The textbox1 text, meaning the source that I am trying to compile is:
class Program
{
static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ShowDialog();
}
}
Basically, I am trying to generate an executable file dynamically, that will just show a Form. I've also tried, instead of making and showing a form to show a System.Windows.MessageBox.Show("testing");
In both cases I get this errors:
Line number 5, Error Number: CS0234, 'The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?);
Line number 5, Error Number: CS0234, 'The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?);
Upvotes: 1
Views: 1442
Reputation: 3095
You are adding 3 files ("System.dll","System.Windows.Forms.dll","System.Drawing.dll") as embedded resources not as references. Add them to ReferencedAssemblies instead.
Upvotes: 6