[Robot]確認某執行檔exe一直不斷執行中,掛掉則會重新啟動他
新增一個console專案之後,加入以下程式碼:
static void Main(string[] args)
{
while (true)
{
try
{
int checkInterval;
Int32.TryParse(
System.Configuration.ConfigurationManager.AppSettings["checkInterval"], out checkInterval);
if (checkInterval <= 0)
{
checkInterval = 60000;
}
Thread.Sleep(checkInterval);
string ProcessName = System.Configuration.ConfigurationManager.AppSettings["ProcessName"];
Process[] pro = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(ProcessName));
if (pro.Length == 0)
{
string ProcessPath = System.Configuration.ConfigurationManager.AppSettings["ProcessPath"];
if (!File.Exists(ProcessPath))
{
Console.WriteLine("\"" + ProcessPath + "\" 不存在");
}
else
{
Process.Start(ProcessPath);
}
}
else
{
Console.Clear();
Console.WriteLine("Process:" + pro[0].ProcessName + " 執行中");
}
}
catch (Exception ex)
{
Console.WriteLine("ex:" + ex.ToString());
Console.ReadLine();
}
}
}
appsettings加入設定:
<appSettings>
<!--Log路徑-->
<add key="ProcessPath" value="D:\YourPath\yourExe.exe"/>
<add key="ProcessName" value="yourExe.exe"/>
<add key="checkInterval" value="60000"/><!--監控間隔 毫秒-->
</appSettings>