2011-04-15 108 views
52

我需要在用戶選擇菜單選項時關閉控制檯。關閉控制檯應用程序的命令?

我嘗試使用close(),但它沒有工作..

我該怎麼辦呢?

+1

只是好奇:你嘗試調用.Close()的哪個對象? – 2011-04-15 21:15:15

回答

21

通過關閉,你的意思是你想要的控制檯應用程序關閉當前實例,或你想要的應用程序,終止?錯過了所有重要的退出代碼:

Environment.Exit(0); 

或者關閉窗體的當前實例:

this.Close(); 

有用link

6

你可以試試這個

Application.Exit(); 
1
//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(); 
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(); 
     } 
相關問題