Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序控制台或Windows窗体应用程序的构成要素是什么?_C#_.net_Winforms_Visual Studio 2008_Console Application - Fatal编程技术网

C# 应用程序控制台或Windows窗体应用程序的构成要素是什么?

C# 应用程序控制台或Windows窗体应用程序的构成要素是什么?,c#,.net,winforms,visual-studio-2008,console-application,C#,.net,Winforms,Visual Studio 2008,Console Application,[Visual Studio 2008] 我为console应用程序创建了一个新项目,并将其修改为如下所示: class Program { static void Main (string[] args) { Thread.Sleep (2000); } } 然后,我为Windows窗体应用程序创建了另一个项目并对其进行了修改: static class Program { //[STAThread] commented this line

[Visual Studio 2008]

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

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}
然后,我为Windows窗体应用程序创建了另一个项目并对其进行了修改:


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.Write等),也没有在第二个应用程序中编写表单相关操作。看起来和我一模一样


第一个应用程序显示黑色窗口,第二个应用程序没有显示任何内容。是什么使它这样工作的?

如果使用ILDASM检查exe文件,您可以看到清单中存在差异(查找“子系统”)

在Winforms应用程序中:

.subsystem 0x0002       // WINDOWS_GUI
.subsystem 0x0003       // WINDOWS_CUI
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
在控制台应用程序中:

.subsystem 0x0002       // WINDOWS_GUI
.subsystem 0x0003       // WINDOWS_CUI
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
IL代码中可能存在更多差异

当谈到编译器在这两种情况下发出不同消息的原因时,这是由项目文件的OutputType值控制的:

在Winforms应用程序中:

.subsystem 0x0002       // WINDOWS_GUI
.subsystem 0x0003       // WINDOWS_CUI
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
WinExe
在控制台应用程序中:

.subsystem 0x0002       // WINDOWS_GUI
.subsystem 0x0003       // WINDOWS_CUI
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
Exe
出于好奇,我还检查了类库项目的值:

<OutputType>Library</OutputType>

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

我相信幕后VS正是弗雷德里克在他的帖子中所展示的


此外,将其设置为Console Application(控制台应用程序)将显示windows窗体项目的黑色控制台应用程序。

如果查看项目文件(csproj),您将看到目标被定义为控制台或windows应用程序。

在引擎盖下,winform与console exe之间没有区别,只是PE头中有一个标志,上面写着“我需要一个控制台”。PE头不是从C#控制的(因为它是编译的东西,而不是运行时的东西),所以它是在项目文件中定义的(

或者在命令行(
csc/target:exe
vs
csc/target:winexe


可以说,他们可以使用编译器截获的汇编级属性,但这真的有帮助吗?可能没有。

我明白你的意思,但我的问题恰恰是:是什么让Visual studio(或编译器)产生了这种差异?应用程序中的任何内容都必须由我们编写的代码进行管理。对吗?对于编译器来说没有区别,这都与您在项目设置和引用中设置的程序集类型有关。由于这些不同的设置,生成的输出将不同,Windows在启动时将以不同的方式处理应用程序。我想您可能会说“OutputType”元素是导致这种区别的“代码”。找到此元素是为了查找Exe和WinExe MSBuild输出类型之间的差异。非常有用,谢谢!同样相关:为了完整性,还可以选择将输出输出到类库。您可以使用适当的编译器开关将任何项目类型编译到程序集。同样相关: