From: misomosi Date: Mon, 18 May 2026 17:14:23 +0000 (-0400) Subject: Add conditional compilation for ProcessRestart X-Git-Url: http://git.misomosispi.com/?a=commitdiff_plain;h=28e2c2e3a73fd90d879cf639186322f7cb9d4caf;p=gists.git Add conditional compilation for ProcessRestart --- diff --git a/DotnetProcessRestart/.gitignore b/DotnetProcessRestart/.gitignore new file mode 100644 index 0000000..ca5b692 --- /dev/null +++ b/DotnetProcessRestart/.gitignore @@ -0,0 +1,7 @@ +# C# .gitignore +*.dll +*.exe + +obj/ +bin/ +lib/ diff --git a/DotnetProcessRestart/ProcessRestart.cs b/DotnetProcessRestart/ProcessRestart.cs new file mode 100644 index 0000000..85af26a --- /dev/null +++ b/DotnetProcessRestart/ProcessRestart.cs @@ -0,0 +1,36 @@ +using System; +using System.Runtime.InteropServices; +class RestartExample { +#if NET5_0_OR_GREATER + public static void Restart() { + System.Console.WriteLine("Core!"); + // Use ProcessStartInfo.ArgumentList to pass args + Environment.Exit(0); + } +#else + // Technically an unsafe API but we keep it private + [DllImport("KERNEL32.dll", ExactSpelling=true, SetLastError=true)] + private static extern int CreateProcessW( + string lpApplicationName, + char[] lpCommandLine, + IntPtr lpProcesAttributes, + IntPtr lpThreadAttributes, + int bInheritHandles, + uint dwCreationFlags, + IntPtr lpEnvironment, + string lpCurrentDirectory, + IntPtr lpStartupInfo, + IntPtr lpProcessInformation + ); + public static void Restart() { + Console.WriteLine("Windows!"); + char[] lpCommandLine = Environment.CommandLine.ToCharArray(); + // Use CreateProcessW with lpCommandLine to relaunch process + Environment.Exit(0); + } +#endif + + public static void Main(string[] args) { + Restart(); + } +} diff --git a/DotnetProcessRestart/ProcessRestart.csproj b/DotnetProcessRestart/ProcessRestart.csproj new file mode 100644 index 0000000..c806240 --- /dev/null +++ b/DotnetProcessRestart/ProcessRestart.csproj @@ -0,0 +1,8 @@ + + + + Exe + net11.0;net45 + + +