"Restart" a console application

C Sharp - C# (programming language)
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

"Restart" a console application

Post by dzcadii » Sun Mar 01, 2015 10:50 pm

Here is a simple way to restart a console application. I'm sure there a better ways but this opens another instance of the allication and kills the current running process.

Namespace used:

Code: Select all

using System.Reflection;

Code: Select all

//Restart the app for another scan if not kill it 
        static void ResetMe(string resOption)
        {
            var fileName = Assembly.GetExecutingAssembly().Location;
            System.Diagnostics.Process.Start(fileName);
            Environment.Exit(0);
        }
Usage:

Code: Select all

Console.WriteLine("Do you want to rerun the application?\r\n1 - Yes\r\n2 - No (Exit/Quit)");
            string resOption = Console.ReadLine();
            if (resOption == "1")
            {
                ResetMe(resOption);
            }
            else if (resOption == "2")
            {
                Environment.Exit(0);
            }
            else
            {
                Environment.Exit(0);
            }
or at any time just call. Error code "0" tells the Operating System (OS) that the application exited normally.
See (System Error Codes) for a list of error codes.

Code: Select all

Environment.Exit(0);

Post Reply