【问题标题】:Command to close an application of console?关闭控制台应用程序的命令?
【发布时间】:2011-08-06 15:31:29
【问题描述】:

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

我尝试使用close(),但它不起作用..

我该怎么做?

【问题讨论】:

标签: c# console console-application


【解决方案1】:

Environment.ExitApplication.Exit

Environment.Exit(0) 更干净。

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

【讨论】:

    【解决方案2】:

    关闭是指要关闭控制台应用程序的当前实例,还是要终止应用程序进程?错过了所有重要的退出代码:

    Environment.Exit(0);
    

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

    this.Close();
    

    有用的link

    【讨论】:

      【解决方案3】:

      你可以试试这个

      Application.Exit();
      

      【讨论】:

        【解决方案4】:
         //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();
        

        【讨论】:

          【解决方案5】:

          所以您并没有说您希望应用程序突然退出或退出,因此作为另一种选择,也许只是让响应循环优雅地结束。 (我假设你有一个等待用户指令的 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();
                  }
          

          【讨论】:

            【解决方案6】:

            return; 将退出 C# 中的方法。

            见下面的代码 sn-p

            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;
                        }
            }
            

            因此,在这种情况下,如果用户输入空字符串或空格,则使用 return 方法可以优雅地终止 Main 方法。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-01-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-14
              • 1970-01-01
              • 2015-10-06
              • 1970-01-01
              相关资源
              最近更新 更多