如何重新启动Console应用程序?

当用户按“R”时,我需要重新启动应用控制台。

我有这个

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

谢谢

我认为你真的不需要重启整个应用程序。 按下R后只需运行所需的方法。无需重启。

 // Starts a new instance of the program itself System.Diagnostics.Process.Start(Application.ExecutablePath); // Closes the current process Environment.Exit(0); 
 static void Main(string[] args) { var info = Console.ReadKey(); if (info.Key == ConsoleKey.R) { var fileName = Assembly.GetExecutingAssembly().Location; System.Diagnostics.Process.Start(fileName); } } 

另一种简单方法

 //Start process, friendly name is something like MyApp.exe (from current bin directory) System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.FriendlyName); //Close the current process Environment.Exit(0); 

启动第二个exe,结束控制台程序,启动一个新实例,并结束自己?

要明确,代码怎么样?

如果这是您想要追求的解决方案,那么此命名空间应该包含您需要的所有内容。

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

每个人都在考虑这个问题。 尝试这样的事情:

 class Program : IDisposable { class RestartException : Exception { public RestartException() : base() { } public RestartException( string message ) : base(message) { } public RestartException( string message , Exception innerException) : base( message , innerException ) { } protected RestartException( SerializationInfo info , StreamingContext context ) : base( info , context ) { } } static int Main( string[] argv ) { int rc ; bool restartExceptionThrown ; do { restartExceptionThrown = false ; try { using ( Program appInstance = new Program( argv ) ) { rc = appInstance.Execute() ; } } catch ( RestartException ) { restartExceptionThrown = true ; } } while ( restartExceptionThrown ) ; return rc ; } public Program( string[] argv ) { // initialization logic here } public int Execute() { // core of your program here DoSomething() ; if ( restartNeeded ) { throw new RestartException() ; } DoSomethingMore() ; return applicationStatus ; } public void Dispose() { // dispose of any resources used by this instance } } 

试试这样:

 // start new process System.Diagnostics.Process.Start( Environment.GetCommandLineArgs()[0], Environment.GetCommandLineArgs()[1]); // close current process Environment.Exit(0); 
 //here the code to restart the console... System.Diagnostics.Process.Start(Environment.GetCommandLineArgs()[0], Environment.GetCommandLineArgs().Length > 1 ? string.Join(" ", Environment.GetCommandLineArgs().Skip(1)) : null);