应用程序控制台或 Windows 窗体应用程序由什么组成?
[Visual Studio 2008]
我为控制台应用程序创建了一个新项目并将其修改为如下所示:
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 等),也没有首先编写控制台函数(Console.Write 等)我也没有在第二个应用程序中编写与表格相关的操作。 看起来和我一模一样。
第一个应用程序仍然显示黑色窗口,第二个应用程序没有显示任何内容。 是什么让它这样工作?
[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);
}
}
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);
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 ILDASM 检查 exe 文件,您会发现清单中存在差异(查找“子系统”)。
在 Winforms 应用程序中:
在控制台应用程序中:
IL 代码可能有更多差异。
当谈到是什么使编译器在两种情况下以不同的方式发出此消息时,这是由项目文件的 OutputType 值控制的:
在 Winforms 应用程序中:
在控制台应用程序中:
出于好奇,我还检查了类库项目的该值:
If you inspect the exe files usine ILDASM you can see that there is a difference in the Manifest (look for "subsystem").
In a Winforms application:
In a console application:
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:
In a Winforms application:
In a console application:
Out of curiosity I also checked that value for a Class Library project:
在项目属性、应用程序选项卡、输出类型中,您可以设置为“Windows 应用程序”或“控制台应用程序”。
我相信 VS 在幕后所做的正是 Fredrik 在他的帖子中所介绍的。
此外,将其设置为控制台应用程序将显示 Windows 窗体项目的黑色控制台应用程序。
In project properties, Application Tab, Output Type you can set to 'Windows Application' or 'Console Application'.
I believe that behind the scenes VS does exactly what Fredrik presented in his post.
Also, setting it to Console Application will show you the black console application for the windows Forms project.
从本质上讲,winform 与控制台 exe 没有什么区别,除了 PE 标头中的一个标志“我需要一个控制台”之外。 PE 标头不受 C# 控制(因为它是编译事物,而不是运行时事物),因此它是在项目文件中定义的 (... )。
或者在命令行(
csc /target:exe
与csc /target:winexe
)。可以说,他们可以使用编译器拦截的程序集级属性 - 但这真的有帮助吗? 可能不会。
Under the bonnet, there is no difference in a winform vs console exe except for a flag in the PE-header that says "I need a console". The PE header is not controlled from your C# (since it is a compile thing, not a runtime thing), so this is defined in the project file instead (
<OutputType>...</OutputType>
).Or at the command-line (
csc /target:exe
vscsc /target:winexe
).Arguably, they could have used an assembly-level attribute that the compiler intercepted - but would that really have helped? Probably not.
如果您查看项目文件 (csproj),您将看到目标被定义为控制台或 Windows 应用程序。
If you look in the project file (csproj) you'll see that the target is defined there as either a console or windows app.