In .NET/C# test if process has administrative privileges

前端 未结 10 1976
天涯浪人
天涯浪人 2020-11-27 03:35

Is there a canonical way to test to see if the process has administrative privileges on a machine?

I\'m going to be starting a long running process, and much later

10条回答
  •  粉色の甜心
    2020-11-27 04:31

    If you want to make sure your solution works in Vista UAC, and have .Net Framework 3.5 or better, you might want to use the System.DirectoryServices.AccountManagement namespace. Your code would look something like:

    bool isAllowed = false;
    using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, null))
    {
        UserPrincipal up = UserPrincipal.Current;
        GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, "Administrators");
        if (up.IsMemberOf(gp))
            isAllowed = true;
    }
    

提交回复
热议问题