是什么使应用程序控制台或Windows窗体应用程序?

Hem*_*ant 14 .net c# console-application visual-studio-2008 winforms

[Visual Studio 2008]

我为控制台应用程序创建了一个新项目,并将其修改为如下所示:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我为Windows Form应用程序创建了另一个项目并进行了修改:


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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我既没有在第一个应用程序中编写Console函数(Console.Write等),也没有在第二个应用程序中编写与表单相关的操作.看起来和我一模一样.

第一个应用程序显示BLACK窗口,第二个应用程序没有显示任何内容.是什么让它像这样工作?

Fre*_*örk 26

如果您检查exe文件usine ILDASM,您可以看到Manifest存在差异(查找"子系统").

在Winforms应用程序中:

.subsystem 0x0002       // WINDOWS_GUI
Run Code Online (Sandbox Code Playgroud)

在控制台应用程序中:

.subsystem 0x0003       // WINDOWS_CUI
Run Code Online (Sandbox Code Playgroud)

IL代码中可能存在更多差异.

当涉及到在两种情况下编译器以不同方式发出这种情况的原因时,这由项目文件的OutputType值控制:

在Winforms应用程序中:

<OutputType>WinExe</OutputType>
Run Code Online (Sandbox Code Playgroud)

在控制台应用程序中:

<OutputType>Exe</OutputType>
Run Code Online (Sandbox Code Playgroud)

出于好奇,我还检查了类库项目的值:

<OutputType>Library</OutputType>
Run Code Online (Sandbox Code Playgroud)

  • 也相关:http://stackoverflow.com/questions/4866352/what-are-the-effects-of-the-pe-header-subsystem-field (3认同)
  • 对于编译器来说没有区别,这一切都与您在项目设置和引用中设置的程序集类型有关。 (2认同)

Ale*_*nea 9

在项目属性,应用程序选项卡,输出类型中,您可以设置为"Windows应用程序"或"控制台应用程序".

我相信在幕后VS完全是Fredrik在帖子中提出的.

此外,将其设置为控制台应用程序将显示Windows窗体项目的黑色控制台应用程序.


Mar*_*ell 7

在引擎盖下,winform与console exe没有区别,只是PE-header中的一个标志显示"我需要一个控制台".PE头不是由你的C#控制的(因为它是编译的东西,而不是运行时的东西),所以这是在项目文件中定义的(<OutputType>...</OutputType>).

或者在命令行(csc /target:exevs csc /target:winexe).

可以说,他们可以使用编译器拦截的汇编级属性 - 但这确实有帮助吗?可能不是.

  • 也相关:http://stackoverflow.com/questions/4866352/what-are-the-effects-of-the-pe-header-subsystem-field (2认同)