public bool IsUserAdministrator(){try{WindowsIdentity user = WindowsIdentity.GetCurrent();WindowsPrincipal principal = new WindowsPrincipal(user);return principal.IsInRole(WindowsBuiltInRole.Administrator);}catch{return false;}}
using System;using System.Diagnostics;using System.Reflection;using System.Security.Principal;
public static class AdminRelauncher{public static void RelaunchIfNotAdmin(){if (!RunningAsAdmin()){Console.WriteLine("Running as admin required!");ProcessStartInfo proc = new ProcessStartInfo();proc.UseShellExecute = true;proc.WorkingDirectory = Environment.CurrentDirectory;proc.FileName = Assembly.GetEntryAssembly().CodeBase;proc.Verb = "runas";try{Process.Start(proc);Environment.Exit(0);}catch (Exception ex){Console.WriteLine("This program must be run as an administrator! \n\n" + ex.ToString());Environment.Exit(0);}}}
private static bool RunningAsAdmin(){WindowsIdentity id = WindowsIdentity.GetCurrent();WindowsPrincipal principal = new WindowsPrincipal(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);}}