问题描述
我试图在Visual Studio C#中打开一个控制台应用程序。一旦我打开它,它会立即关闭。
I am trying to open a console application in visual studio built in C#. As soon as I open it, it closes immediately.
我知道窗口集这是一个安全默认(至少我认为)。如何解决这个问题?
I know windows sets this is a a safety default(atleast I think). How do I fix this?
我知道我可以编译它并创建一个快捷方式并修改目标,使它有应用程序位置之前的命令提示符的位置。虽然创建它的程序员在Visual studio的输出中产生了信息,所以我必须在那里打开它。
I know I can compile it and create a shortcut and modify the target so it has the location of the command prompt in it before the applications location. Although the programmer who created this has it generating information into the output of visual studio, so it's imperative that I only open it there.
它发生在大多数应用程序,而不只是在Visual Studio中,只是在这种情况下我需要它在VS 2010中打开。我使用Windows 7 .Thanks。
It happens with most applications and not just in visual studio, just in this case I need it to open in VS 2010. I am using Windows 7 .Thanks.
推荐答案
这是一个古老的问题,并启发了几个有趣的漫画:
This is an ancient problem and has inspired several funny cartoons:
让我们来解决。您想要做的是当控制台应用程序从桌面上的快捷方式,Windows资源管理器或Visual Studio启动时提示用户按任意键。但是不是当它从命令处理器启动时运行它自己的控制台。你可以用一个pinvoke这样做,你可以找出过程是否是控制台窗口的唯一所有者,像这样:
Let's fix it. What you want to do is prompt the user to press the Any key when the console app was started from a shortcut on the desktop, Windows Explorer or Visual Studio. But not when it was started from the command processor running its own console. You can do so with a little pinvoke, you can find out if the process is the sole owner of the console window, like this:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Working on it...");
//...
Console.WriteLine("Done");
PressAnyKey();
}
private static void PressAnyKey() {
if (GetConsoleProcessList(new int[2], 2) <= 1) {
Console.Write("Press any key to continue");
Console.ReadKey();
}
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int GetConsoleProcessList(int[] buffer, int size);
}
这篇关于控制台应用程序在visual studio中打开后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!