如何授予管理员权限代码? [英] How to give Administrator Rights to code?

查看:132
本文介绍了如何授予管理员权限代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





朋友我正在开发一个名为计算机扫描仪的项目,但是当我运行我的代码以扫描我的硬盘时,它给了我一个例外访问被拒绝访问文档和设置文件夹,我使用的一些代码可以让我以管理员身份运行我的代码,但仍然向我显示相同的异常。



代码为:

Hi,

Friends I am working on a project called computer scanner, But when i run my code in order to scan my hard drives its gives me an exception "Access Denied to Documents and Setting Folders" , I am using some code which would allow me to run my code as a Administrator, but still showing me the same exception.

The codes are:

WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
                WindowsPrincipal CurrentPrincipal = new      WindowsPrincipal(CurrentIdentity);

                if (CurrentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
//Scanning Code will Execute
}




使用此代码后
它仍然给我一个例外...



请帮助。



after using this code it still giving me an exception...

Please Help.

推荐答案

您需要向项目添加自定义清单,然后将其包含在项目的属性中。清单是一个简单的xml文件,格式如下:

You will need to add a custom manifest to your project and then include it into the project''s properties. The manifest is a simple xml file in the following format:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">
  <assemblyidentity version="1.0.0.0" name="MyApplication.app" />
  <trustinfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedprivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedexecutionlevel level="requireAdministrator" uiaccess="false" />
      </requestedprivileges>
    </security>
  </trustinfo>
</assembly>



你可以找到d项目的efault清单文件位于 bin \Debug bin \Release 目录中,因此您可以使用其中一个作为自定义文件的基础。关键更改位于 节点,其中 requestedExecutionLevel级别必须设置为 requireAdministrator ,如上面的示例所示。


You can find the default manifest file for your project in the bin\Debug and bin\Release directories, so you can use one of those as the basis for your custom file. The key change is in the node, where the requestedExecutionLevel level must be set equal to requireAdministrator as shown in the sample above.


听起来您遇到的问题是用户没有管理员权限或UAC对话框不在触发,因此应用程序不以管理员权限运行。如果您的问题是第一个(没有管理员权限),您将需要使用模拟:



http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-c [< a href =http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-ctarget =_ blanktitle =New Window> ^ ]



如果问题是第二个问题(UAC),则需要在启动代码之前启动UAC提示:



http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7 [ ^ ]



这些选项都不是真的那样reat,但他们会工作。但是,更好的选择是将您的应用程序作为服务运行。这样,您可以选择运行它的用户拥有的访问权限级别,并且您不会被UAC阻止。不要忘记,您也可以通过右键单击(或右键单击)并指定用户来以不同的用户(或管理员)身份运行应用程序。
It sounds like you are running into an issue where either the user does not have admin rights or the UAC dialog is not being triggered so the application does not run with admin rights. If your issue is the first one (no admin rights), you will need to work with impersonation:

http://stackoverflow.com/questions/1168571/run-code-as-a-different-user-c[^]

If the problem is the second one (UAC), you will need to kick off the UAC prompt before you fire your code:

http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7[^]

Neither of these options are really that great, but they will work. However, the better option would be to run your application as a service. That way you can choose what level of access rights the user has who is running it and you won''t be stopped by the UAC. Don''t forget that you can also run the application as a different user (or administrator) by right-clicking on it (or shift-right-click) and specifying the user.


只需更改管理员权限:)
Just Change the Administrator Rights :)


这篇关于如何授予管理员权限代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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