问题描述
我想显示一些额外的UI元素的时候,而不是当它不是,类似的工作室在其标题栏中显示2008年管理员怎么视觉时,作为管理员运行的进程正在以管理员身份运行。我怎么知道?
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 'Administrator' in its title bar when running as admin. How can I tell?
推荐答案
从技术上讲,如果你想看看如果成员是本地管理员的帐户的,那么你就可以得到的的当前用户通过<$c$c>User在 的WindowsIdentity
属性类,像这样(静态 GetCurrent
方法获取当前Windows用户):
Technically, if you want to see if the member is the local administrator account, then you can get the security identifier (SID) of the current user through the User
property on the WindowsIdentity
class, like so (the static GetCurrent
method gets the current Windows user):
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string sid = windowsIdentity.User.ToString();
在用户
属性返回其有许多的用户的SID $ P $各种组和用户 pdefined值。
The User
property returns the SID of the user which has a number of predefined values for various groups and users.
然后你会检查,看the SID有以下模式,表明它是本地管理员帐户(这是一个众所周知的SID):
S-1-5 - {其他SID部分} -500
或者,如果你不想来解析字符串,你可以使用<$c$c>SecurityIdentifier类:
Or, if you don't want to parse strings, you can use the SecurityIdentifier
class:
// Get the built-in administrator account.
var sid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid,
null);
// Compare to the current user.
bool isBuiltInAdmin = (windowsIdentity.User == sid);
不过,我怀疑你的真正的想知道的是,如果当前用户是Administrators组的成员的集团的本地机器。您可以使用得到这个SID的<$c$c>WellKnownSidType对 BuiltinAdministratorsSid
:
However, I suspect that what you really want to know is if the current user is a member of the administrators group for the local machine. You can get this SID using the WellKnownSidType
of BuiltinAdministratorsSid
:
// Get the SID of the admin group on the local machine.
var localAdminGroupSid = new SecurityIdentifier(
WellKnownSidType.BuiltinAdministratorsSid, null);
然后你可以检查<$c$c>Groups在的WindowsIdentity
的用户属性,查看该用户是本地管理员组的成员,像这样:
Then you can check the Groups
property on the WindowsIdentity
of the user to see if that user is a member of the local admin group, like so:
bool isLocalAdmin = windowsIdentity.Groups.
Select(g => (SecurityIdentifier) g.Translate(typeof(SecurityIdentifier))).
Any(s => s == localAdminGroupSid);
这篇关于我怎样才能知道我的过程中以管理员身份运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!