Lệnh đóng một ứng dụng của bảng điều khiển?


82

Tôi cần đóng bảng điều khiển khi người dùng chọn một tùy chọn menu.

Tôi đã thử sử dụng close()nhưng nó không hoạt động ..

tôi có thể làm cái này như thế nào?


2
Chỉ tò mò: Bạn đã thử gọi .Close () trên đối tượng nào?
Paul Sasik

Câu trả lời:



28

Tóm lại, ý bạn là bạn muốn phiên bản hiện tại của ứng dụng console đóng lại hay bạn muốn quá trình ứng dụng chấm dứt? Bỏ lỡ tất cả mã thoát quan trọng:

Environment.Exit(0);

Hoặc để đóng phiên bản hiện tại của biểu mẫu:

this.Close();

Liên kết hữu ích .


6

Bạn có thể thử cái này

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; sẽ thoát khỏi một phương thức trong C #.

Xem đoạn mã bên dưới

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

Vì vậy, trong trường hợp này nếu người dùng nhập một chuỗi rỗng hoặc khoảng trắng, thì việc sử dụng phương thức trả về sẽ chấm dứt phương thức Main một cách thanh lịch.


0

Vì vậy, bạn đã không nói rằng bạn muốn ứng dụng thoát hoặc thoát đột ngột, vì vậy, là một tùy chọn khác, có lẽ chỉ cần kết thúc vòng lặp phản hồi một cách trang nhã. (Tôi giả sử bạn có một vòng lặp trong khi chờ hướng dẫn người dùng. Đây là một số mã từ một dự án tôi vừa viết hôm nay.

        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();
        }
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.