我想在进程以管理员身份运行时显示一些额外的 UI 元素,而不是在进程以管理员身份运行时显示一些额外的 UI 元素,类似于 Visual Studio 2008 在以管理员身份运行时在其标题栏中显示“管理员”的方式。我怎么知道?
最佳答案
从技术上讲,如果您想查看该成员是否是本地管理员帐户,那么您可以获得security identifier (SID)当前用户通过 User
property在 WindowsIdentity
class 上,像这样(静态 GetCurrent
method 获取当前 Windows 用户):
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string sid = windowsIdentity.User.ToString();
User
属性返回用户的 SID has a number of predefined values for various groups and users .
S-1-5-{other SID parts}-500
或者,如果你不想解析字符串,你可以使用 SecurityIdentifier
类:
// Get the built-in administrator account.
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid,
null);
// Compare to the current user.
bool isBuiltInAdmin = (windowsIdentity.User == sid);
但是,我怀疑您真正想知道的是当前用户是否是本地机器组 管理员的成员。您可以使用 WellKnownSidType
获取此 SID BuiltinAdministratorsSid
:
// Get the SID of the admin group on the local machine.
var localAdminGroupSid = new SecurityIdentifier(
WellKnownSidType.BuiltinAdministratorsSid, null);
然后你可以检查Groups
property在用户的 WindowsIdentity
上查看该用户是否是本地管理员组的成员,如下所示:
bool isLocalAdmin = windowsIdentity.Groups.
Select(g => (SecurityIdentifier) g.Translate(typeof(SecurityIdentifier))).
Any(s => s == localAdminGroupSid);
关于c# - 如何判断我的进程是否以管理员身份运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/509292/