0

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...

3
  • 3
    What part does not work? Commented Apr 17, 2010 at 18:54
  • ok, when i create a new form in visual studio, then copy all the code inside the Form1.cs file and paste it into my app, i get errors, tonnes of them saying "The type or namespace 'ComponentModel' does not exist in the namespace 'System' (are you missing an assembly reference?), and The type or namespace 'Windows' does not exist in the namespace 'System' ETC. There is one error for each using directive. Commented Apr 17, 2010 at 19:00
  • I apologise if that comment above came across a little rude lastnight, It wasn't my intention Commented Apr 18, 2010 at 3:57

3 Answers 3

3

You have the same problem I did.

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....

Sign up to request clarification or add additional context in comments.

1 Comment

...next time maybe say the solution angers you in a different way.. cheers!
1

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.

5 Comments

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.
Call CompileAssemblyFromFile, and pass the names of both filenames: provider.CompileAssemblyFromFile(options, "Form1.cs", "Form1.Designer.cs");
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?
you of course need to have an entry point specified for your program. (in a new forms project usually in program.cs)
oh my god, i feel so STUPID. I completely forgot about program.cs. So sorry
1

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

8 Comments

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.
How did you add the references ? You need to add them to the ReferencedAssemblies property of the CompilerParameters
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 :)
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
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? ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.