Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in C# if running as local administrator [duplicate]

Possible Duplicate:
How can I tell if my process is running As Administrator?

How can I check using C# if my process is running as the local Administrator?

I know how to find out if the current user is a member of the builtin Administrators group. But this is not what I want to know. I want to know if the current user is the (one-and-only) special local Administrator account.

I do also know how to retrieve the name of the current user, but I do not want to compare that to the hardcoded name "Administrator" because this will not work with localized versions of Windows (e. g. "Administrador" in Spanish, "Administrateur" in French etc.).

like image 547
candritzky Avatar asked Nov 12 '22 22:11

candritzky


1 Answers

this is the way i use

    WindowsIdentity user = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(user);
    bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
like image 88
Subhash Lama Avatar answered Nov 15 '22 11:11

Subhash Lama