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

| 当用户选择菜单选项时,我需要关闭控制台。 我尝试使用
close()
,但是没有用。 我怎样才能做到这一点?     
已邀请:
Environment.Exit
Application.Exit
Environment.Exit(0)
比较干净。 http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx     
通过关闭,您是要关闭控制台应用程序的当前实例,还是要终止应用程序进程?错过了所有重要的退出代码:
Environment.Exit(0);
或关闭表单的当前实例:
this.Close();
有用的链接。     
你可以试试看
Application.Exit();
    
 //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();
    
因此,您并不是说您希望应用程序突然退出或退出,所以作为另一种选择,也许只是让响应循环优雅地结束了。 (我假设您有一个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();
        }
    

要回复问题请先登录注册