생각만큼 어렵지 않습니다.
public static void RestartMe(string commandLine)
{
var myId = Process.GetCurrentProcess().Id;
var myPath = Assembly.GetEntryAssembly().CodeBase.Replace("file:///", "");
var systemPath = typeof(object).Assembly.CodeBase.Replace("file:///", "");
var tempPath = Path.GetTempFileName();
File.WriteAllText(tempPath + ".cs", @"
using System;
using System.Diagnostics;
public class App
{
public static void Main(string[] args)
{
try { Process.GetProcessById(" + myId + @").WaitForExit(); } catch {}
Process.Start(""" + myPath + @""", Environment.CommandLine);
}
}");
var compiler = new ProcessStartInfo
{
FileName = Path.Combine(Path.GetDirectoryName(systemPath), "csc.exe"),
Arguments = tempPath + ".cs",
WorkingDirectory = Path.GetDirectoryName(tempPath),
WindowStyle = ProcessWindowStyle.Hidden,
};
var restarter = new ProcessStartInfo
{
FileName = tempPath + ".exe",
Arguments = commandLine,
WindowStyle = ProcessWindowStyle.Hidden,
};
Process.Start(compiler).WaitForExit();
Process.Start(restarter); // No WaitForExit: restarter WaitForExits us instead
File.Delete(tempPath);
File.Delete(tempPath + ".cs");
Environment.Exit(0);
}
작동 원리 : 이것은 실제로 또 다른 "재시작 자"프로그램을 만들고 수행하지만 고통없이 자동으로 수행하면 다시 시작한 예를 들어 명령 행에서 통과, 다음의 메소드를 호출하기 만하면됩니다. restarter 프로그램에는 현재 프로세스 ID와 실행 파일 이름이 들어 있습니다. NET Framework 버전은 System.dll과 동일한 폴더에 호환되는 csc.exe와 함께 제공되므로 항상 컴파일러를 찾습니다.
많은 살인 - 자살 사례가 있으며, 두 번째 시도는 자살 살인 사건이없는 이유를 설명합니다. – SWeko