C# Obtain Admin right programmaticaly


SUBMITTED BY: Guest

DATE: Nov. 24, 2014, 9:33 a.m.

FORMAT: Text only

SIZE: 995 Bytes

HITS: 1183

  1. public static void SetPermisions()
  2. {
  3. if (!IsAdmin())
  4. {
  5. var exeName = Process.GetCurrentProcess().MainModule.FileName;
  6. ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
  7. startInfo.Verb = "runas";
  8. startInfo.UseShellExecute = true;
  9. Process.Start(startInfo);
  10. return;
  11. }
  12. else
  13. {
  14. Console.WriteLine("WE have Admin rights");
  15. }
  16. }
  17. public static bool IsAdmin()
  18. {
  19. WindowsIdentity id = WindowsIdentity.GetCurrent(); //get the current identity of the current user
  20. WindowsPrincipal p = new WindowsPrincipal(id); //setup a new principal opbject with the above id
  21. return p.IsInRole(WindowsBuiltInRole.Administrator); //return the state of the above principal objects role
  22. }

comments powered by Disqus