问题描述
我正在尝试使用 InstallUtil.exe 安装服务,但通过 Process.Start
调用.代码如下:
I'm trying to install a service using InstallUtil.exe but invoked through Process.Start
. Here's the code:
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
System.Diagnostics.Process.Start (startInfo);
其中 m_strInstallUtil
是InstallUtil.exe"的完全限定路径和 exe,strExePath
是我的服务的完全限定路径/名称.
where m_strInstallUtil
is the fully qualified path and exe to "InstallUtil.exe" and strExePath
is the fully qualified path/name to my service.
从提升的命令提示符运行命令行语法有效;从我的应用程序运行(使用上面的代码)没有.我假设我正在处理一些进程提升问题,那么我将如何在提升状态下运行我的进程?我需要为此查看 ShellExecute
吗?
Running the command line syntax from an elevated command prompt works; running from my app (using the above code) does not. I assume I'm dealing with some process elevation issue, so how would I run my process in an elevated state? Do I need to look at ShellExecute
for this?
这一切都在 Windows Vista 上.我在提升到管理员权限的 VS2008 调试器中运行该进程.
This is all on Windows Vista. I am running the process in the VS2008 debugger elevated to admin privilege.
我也尝试设置 startInfo.Verb = "runas";
但似乎没有解决问题.
I also tried setting startInfo.Verb = "runas";
but it didn't seem to solve the problem.
推荐答案
您可以通过将 startInfo 对象的 Verb 属性设置为 'runas' 来指示应以提升的权限启动新进程,如下所示:
You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:
startInfo.Verb = "runas";
这将导致 Windows 的行为就像是从资源管理器中使用以管理员身份运行"菜单命令启动了进程一样.
This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.
这确实意味着将出现 UAC 提示并且需要用户确认:如果这是不希望的(例如因为它会发生在一个冗长的过程中间),您需要运行整个通过创建和嵌入应用程序清单 (UAC) 提升权限的主机进程要求 'highestAvailable' 执行级别:这将导致 UAC 提示在您的应用启动后立即出现,并导致所有子进程以提升的权限运行而无需额外提示.
This does mean the UAC prompt will come up and will need to be acknowledged by the user: if this is undesirable (for example because it would happen in the middle of a lengthy process), you'll need to run your entire host process with elevated permissions by Create and Embed an Application Manifest (UAC) to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.
我看到您刚刚编辑了您的问题以说明runas"对您不起作用.这真的很奇怪,因为它应该(在几个生产应用程序中对我来说也是如此).不过,通过嵌入清单来要求父进程以提升的权限运行肯定是可行的.
I see you just edited your question to state that "runas" didn't work for you. That's really strange, as it should (and does for me in several production apps). Requiring the parent process to run with elevated rights by embedding the manifest should definitely work, though.
这篇关于以编程方式提升进程特权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!