windows 程序如何请求管理员权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/90702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does a program ask for administrator privileges?
提问by Niyaz
I am working on a small application in VB.NET. The program needs administrator privilege for doing some tasks. Is there a way to ask for administrator privileges during the execution if the program?
我正在 VB.NET 中开发一个小应用程序。该程序需要管理员权限才能执行某些任务。有没有办法在程序执行过程中要求管理员权限?
What is the general way of changing the user account under which the application is running?
更改运行应用程序的用户帐户的一般方法是什么?
回答by J D OConal
回答by 1800 INFORMATION
There are a number of methods depending on your needs. Some details are given in the application developer requirements for UAC.
根据您的需要,有多种方法。UAC 的应用程序开发人员要求中提供了一些详细信息。
- Include a UAC manifest that causes your program to require administrator privileges at startup.
- Use one of the suggested methods for invoking an elevation to run out of process. One of the nicest is to use the COM elevation monikerand
CoCreateInstanceAsAdmin
to call methods on a COM object running as an administrator. This is possibly tricky to get working in VB.Net. I got it working ok in C++ though - Another ok method is to isolate the parts of your code that need admin privileges into an application that uses a UAC manifest to require admin privileges. Your main app does not need to run as an admin in that case. When you require admin privilegese, you would invoke the external application.
- 包括一个 UAC 清单,使您的程序在启动时需要管理员权限。
- 使用建议的方法之一调用提升以运行进程。最好的方法之一是使用COM 提升名称并
CoCreateInstanceAsAdmin
在以管理员身份运行的 COM 对象上调用方法。这可能很难在 VB.Net 中工作。我在 C++ 中运行正常 - 另一种可行的方法是将需要管理员权限的代码部分隔离到使用 UAC 清单以要求管理员权限的应用程序中。在这种情况下,您的主应用程序不需要以管理员身份运行。当您需要管理员权限时,您将调用外部应用程序。
回答by AnOnYmOuS
Try
Dim procInfo As New ProcessStartInfo()
procInfo.UseShellExecute = True
procInfo.FileName = 'Filename here
procInfo.WorkingDirectory = ""
procInfo.Verb = "runas"
Process.Start(procInfo)
Catch ex As Exception
MsgBox(ex.Message.ToString(), vbCritical)
End Try
End If
回答by llk
The most easy way to do this is to click on the Project tab -> Add Windows Form -> .XML file -> name it (program name).manifest -> paste this code in this linkinto it ( thanks JDOConal ) -> then right click on your project name in the solution explorer box off to the right and hit properties -> on the first tab select manifest and then the .manifest file you created -> build = done!
最简单的方法是单击项目选项卡 -> 添加 Windows 窗体 -> .XML 文件 -> 命名(程序名称)。清单 -> 将此链接中的代码粘贴到其中(感谢 JDOConal) ->然后在右侧的解决方案资源管理器框中右键单击您的项目名称并点击属性 -> 在第一个选项卡上选择清单,然后选择您创建的 .manifest 文件 -> 构建 = 完成!