C# 为什么WindowsPrincipal.IsInRole总是为“0”返回false;“行政人员”;团体?

C# 为什么WindowsPrincipal.IsInRole总是为“0”返回false;“行政人员”;团体?,c#,winforms,windows-principal,C#,Winforms,Windows Principal,我的本地用户帐户在Administrators组中,我只想知道windows窗体项目如何确定我是否在Administrators组中。因此,我启动了一个windows窗体项目,并尝试了以下操作: [STAThread] static void Main() { string adminGroup1 = @"BUILTIN\Administrators"; string adminGroup2 = Environment.MachineName + @"\Administrator

我的本地用户帐户在Administrators组中,我只想知道windows窗体项目如何确定我是否在Administrators组中。因此,我启动了一个windows窗体项目,并尝试了以下操作:

[STAThread]
static void Main()
{
    string adminGroup1 = @"BUILTIN\Administrators";
    string adminGroup2 = Environment.MachineName + @"\Administrators";
    string adminGroup3 = Environment.MachineName.ToLower() + @"\Administrators";
    string adminGroup4 = "Administrators";
    string adminGroup5 = "administrators";

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    WindowsPrincipal currentUser1 = (WindowsPrincipal)Thread.CurrentPrincipal;
    bool IsAdmin1_1 = currentUser1.IsInRole(adminGroup1); // false
    bool IsAdmin1_2 = currentUser1.IsInRole(adminGroup2); // false
    bool IsAdmin1_3 = currentUser1.IsInRole(adminGroup3); // false
    bool IsAdmin1_4 = currentUser1.IsInRole(adminGroup4); // false
    bool IsAdmin1_5 = currentUser1.IsInRole(adminGroup5); // false

    WindowsPrincipal currentUser2 = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    bool IsAdmin2_1 = currentUser2.IsInRole(adminGroup1); // false
    bool IsAdmin2_2 = currentUser2.IsInRole(adminGroup2); // false
    bool IsAdmin2_3 = currentUser2.IsInRole(adminGroup3); // false
    bool IsAdmin2_4 = currentUser1.IsInRole(adminGroup4); // false
    bool IsAdmin2_5 = currentUser2.IsInRole(adminGroup5); // false

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
为什么上述所有检查都失败了

试试看

currentUser1.IsInRole(WindowsBuiltInRole.Administrator)


在Windows Vista和更高版本的Windows操作系统中,用户帐户控制(UAC)确定用户的权限。[..]执行IsInRole方法的代码不会显示“同意”对话框。如果您是标准用户角色,即使您是内置管理员组,代码也会返回false

您使用的是英文版Windows吗?否则,您必须为“Administrators”组指定本地化名称。UAC处于启用状态,因此在出现提示之前您不是管理员?以管理员身份运行代码以证明。@TonyHopkinson您是对的。在使用提升的权限启动VS后,检查工作正常。这仍然返回false?