Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,515 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,751 people online right now. Registration is fast and FREE... Join Now!




Compile at runtime

 
Reply to this topicStart new topic

Compile at runtime

Korupt
15 Aug, 2008 - 05:32 PM
Post #1

D.I.C Head
Group Icon

Joined: 22 Jun, 2008
Posts: 58



Thanked: 1 times
Dream Kudos: 25
My Contributions
Could someone help me do this, im just trying to make my app compile an exe from a string in the source code, here's what I'm doing:

csharp
        protected string do_compile(string[] code, string outputFile, bool isexe)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameters;

// These are the equivalent of the references part of a visual studio project
string[] refrences = { "System", "System.Data", "System.Windows.Forms" };

// Create params for compile
parameters = new CompilerParameters(refrences, outputFile, true);
parameters.GenerateExecutable = isexe;

// Send it all to the compiler
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

String result;

// Check for errors
if (results.Errors.Count > 0)
{ // We had an error
result = "Error";

// Add the erorrs to the result
foreach (CompilerError ce in results.Errors)
{
result += "\n" + ce.ToString();
MessageBox.Show(result);
}
}
else
{ // All was fine
result = "Success";
MessageBox.Show(result);
}

return result;
}

private void button1_Click(object sender, EventArgs e)
{
string[] code = { "MessageBox.Show('Hi');" };
string output = @"E:\test.exe";

do_compile(code, output, true);
}


however I get errors (the program complies fine but once I click the button) saying "Metadata file System could not be found, Metadata file System.Data could not be found, Metadata file System.Windows.Forms could not be found" Anyone have an idea on how to make it work? Thanks
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:07PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month