C# Compiler and Windows.Forms
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 g开发者_如何学Pythonives me heaps of errors, and does not produce an exe (obviously). But, my application always successfully compiles straightup console apps...
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....
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.
You just need to add the System
, System.Drawing
and System.Windows.Forms
assemblies as references when you compile
精彩评论