Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static bool checkAdministrator(String args)
- {
- WindowsIdentity identity = WindowsIdentity.GetCurrent();
- WindowsPrincipal principal = new WindowsPrincipal(identity);
- var isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
- if (isAdmin == false)
- {
- try
- {
- var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
- ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
- startInfo.Arguments = args;
- startInfo.Verb = "runas";
- System.Diagnostics.Process.Start(startInfo);
- Application.Exit();
- }
- catch
- {
- DialogResult errormsg = MessageBox.Show("There was an error gaining administrative privileges", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
- if (errormsg == DialogResult.Retry)
- {
- checkAdministrator(args);
- }
- }
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement