是什么让一个应用程序控制台或Windows窗体应用程序?

问题描述:

[Visual Studio 2008中]

[Visual Studio 2008]

我创建的控制台应用程序一个新的项目,并修改它看起来是这样的:

I created a new project for console application and modified it to look like this:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}

然后,我创建了另一个项目的Windows窗体应用程序,并修改了它:

Then I created another project for Windows Form application and modified it:


static class Program
{
    //[STAThread] commented this line
    static void Main (string[] args) { //Added args
        //Commented following lines
        //Application.EnableVisualStyles ();
        //Application.SetCompatibleTextRenderingDefault (false);
        //Application.Run (new Form1 ()); commented this line
        Thread.Sleep (2000);
    }
}

现在我既没有书面Console功能(Console.Write等)的第一个应用程序或我都写在第二个相关的操作形式。看起来与我。

Now I have neither written Console functions (Console.Write etc.) in first application nor I have written forms related operations in second one. Looks identical to me.

不过首次申请显示黑色窗口和第二个没有显示任何东西。是什么让这样的工作?

Still first application shows BLACK window and second one doesn't show anything. What makes it work like this?

如果您检查exe文件usine ILDASM你可以看到,有(寻找子系统)。

If you inspect the exe files usine ILDASM you can see that there is a difference in the Manifest (look for "subsystem").

在一个WinForms应用程序:

In a Winforms application:

.subsystem 0x0002       // WINDOWS_GUI

在一个控制台应用程序:

In a console application:

.subsystem 0x0003       // WINDOWS_CUI

有可能是在IL code更differencies。

There may be more differencies in the IL code.

当谈到是什么让编译器发出此不同的方式在这两种情况下,这是由项目文件的输出类型值控制:

When it comes to what makes the compiler emit this differently in the two cases, this is controlled by the project file's OutputType value:

在一个WinForms应用程序:

In a Winforms application:

<OutputType>WinExe</OutputType>

在一个控制台应用程序:

In a console application:

<OutputType>Exe</OutputType>

出于好奇,我也检查了价值一个类库项目:

Out of curiosity I also checked that value for a Class Library project:

<OutputType>Library</OutputType>