[英]C# How to check for administrator rights / privileges
I want to create a folder with my C# app. 我想用我的C#app创建一个文件夹。 I'm writing code, for me all is ok, but my friend gets an error
我正在编写代码,对我来说一切都很好,但是我的朋友得到了一个错误
Access to the path 'myfolder' is denied.
访问路径'myfolder'被拒绝。
So how can I ask for admin rights? 那我怎么能要求管理员权限? I've searched that it can be created with manifest.. What's it?
我搜索过它可以用清单创建..它是什么? How/where can I get manifest?
如何/在哪里可以获得清单? I'm newbie sorry.
我是新手抱歉。 I want something like this:
我想要这样的东西:
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
建议1检查管理员权限 - 这个是你想在检查后用代码中的bool值做某事的
This will return a bool value and allow you to do what you want with the isAdmin
这将返回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.
建议2 - 强制我的.NET应用程序以管理员身份运行 - 如果您始终希望以管理员身份运行并且我建议使用它,那么我会这样做。
You'll want to modify the manifest that gets embedded in the program. 您将要修改嵌入在程序中的清单。 This works on VS2008 and higher: Project + Add New Item, select "Application Manifest File".
这适用于VS2008及更高版本:Project + Add New Item,选择“Application Manifest File”。 Change the
<requestedExecutionLevel>
element to: 将
<requestedExecutionLevel>
元素更改为:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
The user gets the UAC prompt when they start the program. 用户在启动程序时会收到UAC提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.