在Visual Studio中打开后,控制台应用程序立即关闭

我正在尝试使用C#内置的Visual Studio打开一个控制台应用程序。一打开它,它就会立即关闭。

我知道Windows设置这是一个安全默认值(至少我认为)。我该如何解决?

我知道我可以编译它,创建快捷方式并修改目标,以便它在应用程序位置之前在其中包含命令提示符。尽管创建此代码的程序员将其生成的信息生成到Visual Studio的输出中,所以必须仅在此处打开它。

它发生在大多数应用程序中,而不仅是在Visual Studio中,在这种情况下,我需要在VS 2010中打开它。我使用的是Windows 7。


智慧大石
浏览 951回答 3
3回答

万千封印

这是一个古老的问题,并激发了一些有趣的漫画:在此处输入图片说明让我们修复它。您要执行的操作是从桌面,Windows资源管理器或Visual Studio上的快捷方式启动控制台应用程序时,提示用户按Any键。但不是从运行自己控制台的命令处理器启动它时。您可以通过一点点操作来做到这一点,可以确定该进程是否是控制台窗口的唯一所有者,如下所示:using System;class Program {&nbsp; &nbsp; static void Main(string[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Working on it...");&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Done");&nbsp; &nbsp; &nbsp; &nbsp; PressAnyKey();&nbsp; &nbsp; }&nbsp; &nbsp; private static void PressAnyKey() {&nbsp; &nbsp; &nbsp; &nbsp; if (GetConsoleProcessList(new int[2], 2) <= 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Press any key to continue");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [System.Runtime.InteropServices.DllImport("kernel32.dll")]&nbsp; &nbsp; private static extern int GetConsoleProcessList(int[] buffer, int size);}

守候你守候我

您也可以通过按(Ctrl + F5)键来运行该应用程序。这将允许它在“发布”模式下运行,默认情况下,您需要按“返回”以关闭窗口。

冉冉说

尝试Console.ReadKey();在Main()方法的末尾添加。这是阻止窗口自行关闭的一种快速而肮脏的方法。
打开App,查看更多内容
随时随地看视频慕课网APP