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?
更改运行应用程序的用户帐户的一般方法是什么?
4 个解决方案
#1
3
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 moniker and
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清单以要求管理员权限的应用程序中。在这种情况下,您的主应用程序不需要以管理员身份运行。当您需要管理员权限时,您将调用外部应用程序。
#2
#3
1
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
#4
0
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 link into 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!
最简单的方法是单击Project选项卡 - > Add Windows Form - > .XML file - > name it(program name).manifest - >将此代码粘贴到此链接中(感谢JDOConal) - >然后在解决方案资源管理器框中右键单击您的项目名称,然后点击属性 - >在第一个选项卡上选择清单,然后选择您创建的.manifest文件 - > build = done!
#1
3
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 moniker and
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清单以要求管理员权限的应用程序中。在这种情况下,您的主应用程序不需要以管理员身份运行。当您需要管理员权限时,您将调用外部应用程序。
#2
7
You can specify this in your application's manifest file.
您可以在应用程序的清单文件中指定它。
Check out this link and this link and this link too.
查看此链接以及此链接和此链接。
#3
1
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
#4
0
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 link into 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!
最简单的方法是单击Project选项卡 - > Add Windows Form - > .XML file - > name it(program name).manifest - >将此代码粘贴到此链接中(感谢JDOConal) - >然后在解决方案资源管理器框中右键单击您的项目名称,然后点击属性 - >在第一个选项卡上选择清单,然后选择您创建的.manifest文件 - > build = done!