检查C#中的管理员权限
我想知道程序是否以管理员身份运行。
用户不必是管理员。我只想知道我的应用程序是否有权编辑某些以管理员身份运行时可编辑的受保护文件。
I want to know if a program is running as administrator.
The user doesn't have to be administrator. I only want to know if my application has rights to edit some secured files that are editable when running as Administrator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将返回一个 bool valid
This will return a bool valid
以下是 @atrljoe 的答案,使用最新的 C# 转换为单行代码:
Here's @atrljoe's answer turned into a one liner using the latest C#:
为了避免
SecurityException
和泄漏,例如不关闭WindowsIdentity.GetCurrent()
,因为WindowsPrincipal
不会显式关闭,而只会创建一个新连接然后才将其关闭。To avoid
SecurityException
, and leaks, like not closingWindowsIdentity.GetCurrent()
, sinceWindowsPrincipal
does not explicitly close, but only creates a new connection and only then closes it.