如何查看用户是否具有管理员权限?

3
我用C#编写了一个程序,可以更改Windows 7登录屏幕的背景。该程序需要在System32文件夹中创建一个新文件夹,并将文件移动到其中。我在自己的电脑上测试没有问题,但在朋友的电脑上测试时,无法创建目录。他的账户类型是管理员,但我想可能是他缺少了我电脑上启用的某些权限。所以我想知道有没有办法检查用户拥有哪些权限?或者是否有绕过这个问题的方法。提前感谢!

https://dev59.com/VXNA5IYBdhLWcg3wHp6B,https://dev59.com/q3A65IYBdhLWcg3w2iep 可能是重复的。 - Rosmarine Popcorn
http://www.davidmoore.info/2011/06/20/how-to-check-if-the-current-user-is-an-administrator-even-if-uac-is-on/ - Hans Passant
2个回答

3

要检测这个,你可以像这样获取一个WindowsIdentity对象:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

然后通过以下方式创建WindowsPrincipal的实例:
WindowsPrincipal principal = new WindowsPrincipal(identity);

最后,通过使用IsInRole()方法来检查它,像这样:

string role = "BUILTIN\\Administrators";
bool IsAdmin = principal.IsInRole(role));

然后您可以使用IsAdmin变量来确定当前用户是否为管理员。

来源 - http://csharptuning.blogspot.com/2007/09/detecting-is-current-user-is.html


2
问题出在用户账户控制(UAC)上。这是一个需要提升权限的操作。即使用户属于管理员组,进程默认获得标准用户令牌。
解决方案是在应用程序清单中添加 requireAdministrator 选项,以便应用程序调用 UAC 提权对话框。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接