I need to restart the app Console when the user press "R".
I have this
Console.WriteLine(message, "Rebuild Log Files"
+ " Press Enter to finish, or R to restar the program...");
string restar = Console.ReadLine();
if(restar.ToUpper() == "R")
{
//here the code to restart the console...
}
thanks
Another simple way
try like this:
I realize that this is 7 years old, but I just came across this. I think actually calling the executable and closing the current program is a bit of a cluge. As stated before, this is being over thought. I think that the cleanest and most modular way is to take everything that is in the
Main
method and make a different method, let's sayRun()
that contains everything that was in theMain
method and then call the newRun()
method from theMain
method or wherever in the code it is desired to re-start the program.So if the
Main
method looked like this:Then just create a
Run()
method and put everything fromMain
into it, like so:Now that the
Run()
method is created and it has all the stuff that was in theMain
method before, just make your main method like so:Now, wherever in the code you want to "re-start" the program, just call the
Run()
method like so:So this is a look at the whole program simplified:
EDIT: When I posted this originally I didn't take into consideration any arguments that might have been passed to the program. To account for this four things need to be changed in my original answer.
declare a global
List<string>
like this:public static List<string> MainMethodArgs = new List<string>();
.In the
Main
method set the value of theMainMethodArgs
list equal to the values passed into theMain
method viaargs
like this:MainMethodArgs = args.ToList();
When creating the
Run()
method change the signature so that it expects astring[]
calledargs
to be passed to it like this:public static void Run(string[] args) { .... }
Wherever in the program the
Run()
method is called, passMainMethodArgs
toRun()
like this:Run(MainMethodArgs.ToArray());
I changed the example below to reflect these changes.
In effect the program is "re-started" without having to re-run the executable and close the existing instance of the program. This seems a lot more "programmer like" to me.
Enjoy.
Launch a second exe that ends the console program, starts a new instance, and ends itself?
be explicit, how is it in code?
This namespace should have everything you need, if that is a solution you want to pursue.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx