[英]How restart the Console app?
當用戶按“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...
}
謝謝
// 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);
}
}
我不認為你真的需要重啟整個應用程序。 按下R后只需運行所需的方法。無需重啟。
另一種簡單方法
//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);
試試這樣:
// 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);
我意識到這已經7歲了,但我剛剛遇到過這個問題。 我認為實際調用可執行文件並關閉當前程序有點麻煩。 如前所述,這是過度思考。 我認為最干凈,最模塊化的方法是采用Main
方法中的所有內容並創建一個不同的方法,假設Run()
包含Main
方法中的所有內容,然后從中調用新的Run()
方法Main
方法或代碼中的任何地方都需要重新啟動程序。
因此,如果Main
方法看起來像這樣:
static void Main(string[] args)
{
/*
Main Method Variable declarations;
Main Method Method calls;
Whatever else in the Main Method;
*/
int SomeNumber = 0;
string SomeString = default(string);
SomeMethodCall();
//etc.
}
然后只需創建一個Run()
方法並將Main
所有內容放入其中,如下所示:
public static void Run()
{
//Everything that was in the Main method previously
/*
Main Method Variable declarations;
Main Method Method calls;
Whatever else in the Main Method;
*/
int SomeNumber = 0;
string SomeString = default(string);
SomeMethodCall();
//etc.
}
既然已經創建了Run()
方法並且它具有之前Main
方法中的所有內容,那么只需將main方法設置為:
static void Main(string[] args)
{
Run();
}
現在,無論在代碼中你想要“重新啟動”程序,只需調用Run()
方法,如下所示:
if(/*whatever condition is met*/)
{
//do something first
//then "re-start" the program by calling Run();
Run();
}
所以這是一個簡化的整個程序:
編輯:當我最初發布這個時,我沒有考慮任何可能傳遞給該程序的參數。 為了解釋這四件事需要在原來的答案中改變。
聲明一個全局List<string>
如下所示:
public static List<string> MainMethodArgs = new List<string>();
。
在Main
方法中,將MainMethodArgs
列表的值設置為等於通過args
傳遞給Main
方法的值,如下所示:
MainMethodArgs = args.ToList();
在創建Run()
方法時,更改簽名,以便它希望將一個名為args
的string[]
傳遞給它,如下所示:
public static void Run(string[] args) { .... }
無論程序中調用Run()
方法,將MainMethodArgs
傳遞給Run()
如下所示:
Run(MainMethodArgs.ToArray());
我更改了下面的示例以反映這些更改。
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExampleConsole
{
class Program
{
public static List<string> MainMethodArgs = new List<string>();
static void Main(string[] args)
{
MainMethodArgs = args.ToList();
Run(MainMethodArgs.ToArray());
}
public static void Run(string[] args)
{
Console.WriteLine("Run() is starting");
Console.ReadLine();
//stuff that used to be in the public method
int MainMethodSomeNumber = 0;
string MainMethodSomeString = default(string);
SomeMethod();
//etc.
}
public static void SomeMethod()
{
Console.WriteLine("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...
Run(MainMethodArgs.ToArray());
}
}
}
}
實際上,程序是“重新啟動”而無需重新運行可執行文件並關閉程序的現有實例。 這對我來說似乎更像“程序員”。
請享用。
啟動第二個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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.