检查当前用户是管理员 [英] Check if the current user is administrator

查看:162
本文介绍了检查当前用户是管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要运行一些脚本,我必须确保用户运行这些是管理员...什么是这样使用C#的最佳方式?

My application needs to run some scripts, and I must be sure that the user running them is an administrator... What is the best way of doing this using C#?

推荐答案

使用 WindowsPrincipal

public static bool IsAdministrator()
{
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

这篇关于检查当前用户是管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆