控制台应用程序立即关闭

控制台应用程序立即关闭

本文介绍了在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.

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

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。

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.

推荐答案

这是

让我们修复它。您要执行的操作是从桌面,Windows资源管理器或Visual Studio上的快捷方式启动控制台应用程序时,提示用户按Any键。但是不是从运行自己控制台的命令处理器启动它时。您可以通过一点点操作就可以做到这一点,可以确定该进程是否是控制台窗口的唯一所有者,如下所示:

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中打开后,控制台应用程序立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:48