Command to close an application of console?

279,317

Solution 1

Environment.Exit and Application.Exit

Environment.Exit(0) is cleaner.

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

Solution 2

By close, do you mean you want the current instance of the console app to close, or do you want the application process, to terminate? Missed that all important exit code:

Environment.Exit(0);

Or to close the current instance of the form:

this.Close();

Useful link.

Solution 3

You can Try This

Application.Exit();

Solution 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();

Solution 5

return; will exit a method in C#.

See code snippet below

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

So in this case if a user enters a null string or whitespace, the use of the return method terminates the Main method elegantly.

Share:
279,317
ale
Author by

ale

Updated on March 29, 2020

Comments

  • ale
    ale about 4 years

    I need to close the console when the user selects a menu option.

    I tried using close() but it did not work..

    how can I do this?