How can I tell if my process is running as Administrator?

后端 未结 5 1011

I would like to display some extra UI elements when the process is being run as Administrator as opposed to when it isn\'t, similar to how Visual Studio 2008 displays \'Admi

5条回答
  •  Happy的楠姐
    2020-11-29 07:39

    I felt it important to note the difficulty I had with attempting to use WellKnownSidType.BuiltinAdministratorsSid per casperOne's answer above. According to the WellKnownSiDType MSDN, BuiltinAdministratorsSid "Indicates a SID that matches the administrator account." So I would expect casperOne's code to work, and guess it likely does in some environments. Unfortunately, it didn't on my Windows 2003 with .NET 2.0 (legacy code). It actually returned S-1-5-32-544 which, according to this article is the sid for the Administrators group. Thus, the comparison fails for me. I will have to do my own string comparison for startswith "S-1-5-21" (that kb 243330 indicates the "21" is included even though the blog referenced above does not) and endswith "500".

提交回复
热议问题