Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not as hard as you think. All you need to do is call the following method, passing in the command line for the restarted instance:</p> <pre><code>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); } </code></pre> <p>How it works: This actually does create another "restarter" program but does it painlessly and automatically. The restarter program has the current process id and the executable filename built right into it. It will always find the compiler because NET Framework version ships with a compatible csc.exe in the same folder as System.dll.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload