关闭控制台应用程序的命令?

97

当用户选择菜单选项时,我需要关闭控制台。

我尝试使用 close() 方法,但没有成功。

我该怎么办?


2
只是好奇:您尝试调用 .Close() 的对象是哪个? - Paul Sasik
这个回答解决了你的问题吗?如何在.NET中指定控制台应用程序的退出代码? - Michael Freidgeim
6个回答

198

极客博客文章有更新链接吗?我在互联网档案馆上没找到它,一直在尝试搜索该网址的部分内容。 - JD136

34

你说的"close"是指希望当前控制台应用程序实例关闭,还是希望应用程序进程终止?重要的退出代码被忽略了:

Environment.Exit(0);

或者关闭当前表单的实例:

this.Close();

有用的链接


6
您可以尝试这个。
Application.Exit();

5
 //How to start another application from the current application
 Process runProg = new Process();
 runProg.StartInfo.FileName = pathToFile; //the path of the application
 runProg.StartInfo.Arguments = genArgs; //any arguments you want to pass
 runProg.StartInfo.CreateNoWindow = true;
 runProg.Start();

 //How to end the same application from the current application
 int IDstring = System.Convert.ToInt32(runProg.Id.ToString());
 Process tempProc = Process.GetProcessById(IDstring);
 tempProc.CloseMainWindow();
 tempProc.WaitForExit();

1

return;会在C#中退出一个方法。

请参考以下代码片段:

using System;

namespace Exercise_strings
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("Input string separated by -");

            var stringInput = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(stringInput))
            {
                Console.WriteLine("Nothing entered");
                return;
            }
}

在这种情况下,如果用户输入了空字符串或空格符,使用返回方法可以优雅地终止主方法。

0

所以你没有说你想让应用程序退出或突然退出,因此作为另一种选择,也许只需优雅地结束响应循环即可。(我假设你有一个等待用户指令的 while 循环。这是我今天刚写的一个项目中的一些代码。

        Console.WriteLine("College File Processor");
        Console.WriteLine("*************************************");
        Console.WriteLine("(H)elp");
        Console.WriteLine("Process (W)orkouts");
        Console.WriteLine("Process (I)nterviews");
        Console.WriteLine("Process (P)ro Days");
        Console.WriteLine("(S)tart Processing");
        Console.WriteLine("E(x)it");
        Console.WriteLine("*************************************");

        string response = "";
        string videotype = "";
        bool starting = false;
        bool exiting = false;

        response = Console.ReadLine();

        while ( response != "" )
        {
            switch ( response  )
            {
                case "H":
                case "h":
                    DisplayHelp();
                    break;

                case "W":
                case "w":
                    Console.WriteLine("Video Type set to Workout");
                    videotype = "W";
                    break;

                case "I":
                case "i":
                    Console.WriteLine("Video Type set to Interview");
                    videotype = "I";
                    break;

                case "P":
                case "p":
                    Console.WriteLine("Video Type set to Pro Day");
                    videotype = "P";
                    break;

                case "S":
                case "s":
                    if ( videotype == "" )
                    {
                        Console.WriteLine("Please Select Video Type Before Starting");
                    }
                    else
                    {
                        Console.WriteLine("Starting...");
                        starting = true;
                    }
                    break;

                case "E":
                case "e":
                    Console.WriteLine("Good Bye!");
                    System.Threading.Thread.Sleep(100);
                    exiting = true;
                    break;
            }

            if ( starting || exiting)
            {
                break;
            }
            else
            {
                response = Console.ReadLine();
            }
        }

        if ( starting )
        {
            ProcessFiles();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接