gpt4 book ai didi

C# 如何检查管理员权限/特权

转载 作者:行者123 更新时间:2023-11-30 14:52:37 27 4
gpt4 key购买 nike

我想用我的 C# 应用程序创建一个文件夹。我正在写代码,对我来说一切正常,但我的 friend 得到一个错误

Access to the path 'myfolder' is denied.

那我怎么申请管理员权限呢?我搜索过它可以用 list 创建。它是什么?我如何/在哪里可以获得 list ?我是新手对不起。我想要这样的东西:

enter image description here

最佳答案

Suggestion 1 to check admin rights - this one is if you want to do something with the bool value within the code after you do your checks

这将返回一个 bool 值并允许您使用 isAdmin 做您想做的事

using System.Security.Principal;

bool isAdmin;
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);

Suggest 2 - force my .NET App to run as administrator - this is the one i would do if you always want it to run as administrator and is the one i would suggest to use.

您需要修改嵌入到程序中的 list 。这适用于 VS2008 及更高版本:Project + Add New Item,选择“Application Manifest File”。更改 <requestedExecutionLevel>元素:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

用户在启动程序时会收到 UAC 提示。

关于C# 如何检查管理员权限/特权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31491183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com