Wednesday, December 17, 2014

[C#] How to detect if application is running with local administrator rights

using System.Security.Principal;

public static Boolean HasAdminRights()
{
    try
    {
        var user = WindowsIdentity.GetCurrent();
        var principal = new WindowsPrincipal(user);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch
    {
        return false;
    }
}

No comments:

Post a Comment