控制台应用程序在Visual Studio开盘后立刻关闭 [英] Console application closes immediatly after opening in visual studio

查看:105
本文介绍了控制台应用程序在Visual Studio开盘后立刻关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开内置在C#中的Visual Studio的控制台应用程序。当我打开它,它会立即关闭。

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中,只是在这种情况下,我需要它在2010年我使用Windows 7 VS打开.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:

让我们来解决它。你想要做的是提示用户preSS时,控制台应用程序从桌面,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开盘后立刻关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆