How can I detect if my process is running UAC-elevated or not?

后端 未结 4 1750
无人及你
无人及你 2020-12-02 18:22

My Vista application needs to know whether the user has launched it \"as administrator\" (elevated) or as a standard user (non-elevated). How can I detect that at run time?

4条回答
  •  醉梦人生
    2020-12-02 18:45

    For those of us working in C#, in the Windows SDK there is a "UACDemo" application as a part of the "Cross Technology Samples". They find if the current user is an administrator using this method:

    private bool IsAdministrator
    {
        get
        {
            WindowsIdentity wi = WindowsIdentity.GetCurrent();
            WindowsPrincipal wp = new WindowsPrincipal(wi);
    
            return wp.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
    

    (Note: I refactored the original code to be a property, rather than an "if" statement)

提交回复
热议问题