Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a GUI application using mono?

Tags:

c#

mono

I wrote a C# program and I will compile like this:

C:\Users\Administrator\Documents\Visual Studio 2005\Projects\GUITest\GUITest>mcs *.cs /r:System.Data,System.Drawing,System.Windows.Forms,..\HtmlAgilityPack.dll

But the output application has a console window. Is there a way to compile the program so that I can get a application without a console window?

like image 428
for1096 Avatar asked Apr 22 '13 03:04

for1096


People also ask

What is Mono ui?

A mostly monochrome and very dark UI theme for Atom.

How do you use mono command?

The mono command executes a compiled Mono program in the virtual machine. mono uses a just-in-time compiler (JIT) to translate the compiled CIL bytecode to machine code for execution. The Hello.exe program can be run with mono Hello.exe.

Can you run Winforms in Linux?

For now, Winforms only stays on windows.


1 Answers

There are two different "modes" or types of Windows applications: console applications and GUI applications. The same goes for managed applications, regardless of how you build them.

Console applications will always display a console window on startup, automatically. You can also write code to display a GUI window (e.g., a form), but this is optional. Either way, the console window will always be displayed.

GUI applications do not display anything on startup. Generally, you write code that displays a GUI window (e.g., a form), but you do not have to. If you do not display anything, you have created what people often refer to as a "background application" because it runs in the background without displaying any UI. That is not possible with a console application, because it displays that ugly console window.

So if you don't want the console window, you don't want a console application. You want a regular GUI application.

Now, the challenge is figuring out how to achieve this using the Mono compiler. Visual Studio exposes this option as a project-level setting. The Mono compiler needs a flag, /target to tell it what type of application to build.

  • /target:exe (the default option) will build a console application
  • /target:winexe will build a GUI application
  • /target:library will build a library (which is not an executable application, but just a chunk of reusable code)

So, change the command you're running at the command line to:

mcs *.cs /target:winexe /r:System.Data,System.Drawing,System.Windows.Forms,...

I believe that you will also need to make sure that you're running a relatively recent version of Mono. The older versions did not include support for creating GUI applications (the /target:winexe switch was not implemented). I know that this is fully supported as of version 2.8, but there's little reason not to use the latest version available.

In case my answer is not authoritative enough for you, you'll find the same quick fix (without the rationale) documented in the Mono WinForms FAQ.

like image 80
Cody Gray Avatar answered Sep 21 '22 05:09

Cody Gray