Vista 下安装服务并添加注册表项
我有一个程序作为桌面应用程序运行,但也可以作为 Windows 服务安装。安装是通过使用 GUI 上的“安装为服务”按钮来完成的。该按钮的事件处理程序如下所示:
ProcessStartInfo psi = new ProcessStartInfo("sc", "description " +
this.ServiceName + " \"" +
((AssemblyDescriptionAttribute)attributes[0]).Description + "\"")
psi.CreateNoWindow = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.Verb = "runas";
Process.Start(psi).WaitForExit();
我现在想做的是在 say: 下添加一些额外的注册表项,
HKLM\SOFTWARE\MyCompany\Services
这也需要提升的权限。但是,如果我使用与上述相同的过程和命令“REG ADD”,UAC 对话框将出现多次 - 我不希望出现这种情况。
那么,安装 Windows 服务并在 HKLM 下添加注册表项且仅出现一次 UAC 提示的最佳方法是什么?
I have a program that runs as a desktop application, but can also be installed as a windows service. The installation is done by using an "install as service" button on the GUI. The event handler for this button looks like this:
ProcessStartInfo psi = new ProcessStartInfo("sc", "description " +
this.ServiceName + " \"" +
((AssemblyDescriptionAttribute)attributes[0]).Description + "\"")
psi.CreateNoWindow = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.Verb = "runas";
Process.Start(psi).WaitForExit();
What I would like to do now is to add some additional registry entries under say:
HKLM\SOFTWARE\MyCompany\Services
which also needs elevated privileges. But if I use the same procedure as described above with the command "REG ADD" the UAC dialog would appear more than once - and I don't want that.
So whats the best way to install a windows service and add a registry entry under HKLM with only having the UAC prompt once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建一个小型 exe 或批处理文件来执行这两项操作(sc 和 reg 更新)。启动that而不是sc。
Create a small exe or a batch file that does both things (the sc and the reg update). Launch that instead of sc.
至于安装服务。
您可以尝试 topshelf。它易于使用且功能非常强大。
As for installing service.
You can try topshelf. It is easy to use and very powerful.
我最终使用管理员权限和特殊参数
myprogram.exe -i
调用自己,然后分支到一个特殊方法,该方法使用ServiceInstaller
类将程序安装为服务(而不是调用 sc)。I ended up calling myself with admin priviliges and a special parameter
myprogram.exe -i
and then branch into a special method which uses theServiceInstaller
class to install the program as a service (instead of calling sc).