Visual Basic 程序 - 请求管理员权限 [英] Visual Basic Program - Ask for Admin Permissions

查看:72
本文介绍了Visual Basic 程序 - 请求管理员权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Visual Basic 编写安装程序,并且我希望安装程序尝试获得管理员权限.如果帐户是管理员,我希望它只是假设他们,如果帐户没有管理员权限,我希望显示这样的提示.

I am trying to write an installer program in Visual Basic, and I would like the installer to try to get Admin permissions. If the account is an administrator, I want it to just assume them, and if the account does not have admin permissions, I want a prompt such as this to show.

如果用户可以授予权限,那么它将继续使用这些权限,如果不能授予权限,那么它将采用替代的、无管理员权限的途径,就像 Google Chrome 安装程序一样.我该怎么做呢?经过一个小时的研究,我在 Google 上没有发现任何有用的信息.

If the user can give the permissions, then it will continue on with them, and if it cannot give them, then it will take an alternative, no-admin permissions route, just like the Google Chrome installer. How do I do this? I haven't found anything helpful on Google after an hour of research.

推荐答案

在 Visual Studio 中,向项目添加一个新文件,选择应用程序清单"作为文件类型,然后单击确定".

In Visual Studio, add a new file to your project, select "application manifest" as the file type and then click okay.

您将获得一个 XML 文档.文档的第一大块是这样的:

You will get an XML document. The first big chunk of the document goes something like this:

 <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

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

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />

取消注释以下行:

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

这将使该应用程序(或 DLL)运行 UAC 以获得管理员权限.

This will make that application (or a DLL) run the UAC to get administrator privileges.

如果您只想提升某个特定组件的运行权限,请将该代码放在单独的 DLL 文件中.当您的主程序首次调用该程序集时,将出现 UAC,并授予提升权限.

If you only want a particular component to run elevated, then put that code in a separate DLL file. When your main program first calls that assembly, the UAC will appear, and elevation will be granted.

这篇关于Visual Basic 程序 - 请求管理员权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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