]> localhost Git - gists.git/commitdiff
Add conditional compilation for ProcessRestart
authormisomosi <[email protected]>
Mon, 18 May 2026 17:14:23 +0000 (13:14 -0400)
committermisomosi <[email protected]>
Mon, 18 May 2026 17:14:23 +0000 (13:14 -0400)
DotnetProcessRestart/.gitignore [new file with mode: 0644]
DotnetProcessRestart/ProcessRestart.cs [new file with mode: 0644]
DotnetProcessRestart/ProcessRestart.csproj [new file with mode: 0644]

diff --git a/DotnetProcessRestart/.gitignore b/DotnetProcessRestart/.gitignore
new file mode 100644 (file)
index 0000000..ca5b692
--- /dev/null
@@ -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 (file)
index 0000000..85af26a
--- /dev/null
@@ -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 (file)
index 0000000..c806240
--- /dev/null
@@ -0,0 +1,8 @@
+<Project Sdk="Microsoft.NET.Sdk">\r
+\r
+  <PropertyGroup>\r
+    <OutputType>Exe</OutputType>\r
+    <TargetFrameworks>net11.0;net45</TargetFrameworks>\r
+  </PropertyGroup>\r
+\r
+</Project>\r