views:

129

answers:

3

Hi All

I've just created a little app that programmatically compiles code using the C# Compiler, and it works brilliantly. But, one thing that I need it to do is compile Windows.Forms code. Like, I can create a console app with it, but I can't create a GUI-based form. Here's the link that got me started:

http://support.microsoft.com/kb/304655

Can somebody please help?

Thank you :)

update

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("hi jason");
        }
    }
}

The above code is what I type into my application. And when I try to compile this code, my application gives me heaps of errors, and does not produce an exe (obviously). But, my application always successfully compiles straightup console apps...

+2  A: 

You just need to add the System, System.Drawing and System.Windows.Forms assemblies as references when you compile

Thomas Levesque
I did. And that's when these errors pop up. Please see my question, I've just updates it to show exactly what I'm typing.
lucifer
How did you add the references ? You need to add them to the `ReferencedAssemblies` property of the `CompilerParameters`
Thomas Levesque
Ohhhhh, that way... I thought you were talking about the other ones (i,e, "Using System.Drawing" at the top of the file).. kk Trying it now :)
lucifer
ok, we're getting somewhere now :). I just did that, and tried to compile. Now it only has 2 errors. "Metadata file 'System' could notbe found" and "Metadata file 'System.Windows.Forms' could not be found". What does that mean? I've never heard of a metadata file in C# before
lucifer
I just added ".dll" to the end of the Referenced Assemblies. Now there's only one error: "InitializeComponent does not exist in the current context.". Does Initializecomponent need some type of other reference somewhere? ...
lucifer
Did you include the .designer.cs file in the compiler inputs ?
Thomas Levesque
I can, but I don't know where to add it. Do I add it to: compilerParams.EmbeddedResources?
lucifer
Just pass it as an extra parameter to the CompileAssemblyFromSource method
Thomas Levesque
+1  A: 

You need to include both the Form1.cs and Form1.Designer.cs to fully compile it. Of course, you have to include references to the Forms and any other needed namespace as well.

Femaref
Can you please suggest how I'd do that? MSDN, or even that article (in my question) doesn't say how to do that. It doesn't even say anything about that.
lucifer
Call CompileAssemblyFromFile, and pass the names of both filenames: `provider.CompileAssemblyFromFile(options, "Form1.cs", "Form1.Designer.cs");`
itowlson
thank you :) BUT, now it says: SampleCompiler.exe does not contain a static 'Main' method suitable for an entry point! ... I've never seen a Main method in a Form1.cs file before... What the?
lucifer
you of course need to have an entry point specified for your program. (in a new forms project usually in program.cs)
Femaref
oh my god, i feel so STUPID. I completely forgot about program.cs. So sorry
lucifer
A: 

You have the same problem I did.

The solution is going to piss you off as much as it did for me....

All you need to do is tell the compiler to compile the program as winexe. To do this, simply add this to your CompilerParams:

CompilerParams.CompilerOptions = "/target:winexe";

But also note, that will compile it with the default icon (which looks horrible!), so you will need to add additional arguments to that:

if (File.Exists(iconPath)) CompilerParams.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" + iConPath + "\""; else CompilerParams.CompilerOptions = "/target:winexe";

This way it will check to see if the icon exists before trying to put it in, to save you a bit of trouble that I had to go through....

Matthew Silber