Check if the current user is administrator

c#, windows-administration

Solution

using System.Security.Principal;

public static bool IsAdministrator()
{
    using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
    {
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
}

Problem

My application needs to run some scripts, and I must be sure that the user running them is an administrator... What is the best way of doing this using C#?

Original source