Windows 服务设置项目 - 以管理员身份运行服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1692679/
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
Windows Service setup project - run service as administrator
提问by db1234
I've got a setup project for a Windows Service (.net 3.5, visual studio 2008).
我有一个 Windows 服务的安装项目(.net 3.5,visual studio 2008)。
The Windows Service needs to be run under the Administrator account, does anyone know how I can get the Setup Project to set the "user to log on as" setting for the windows service as part of the setup process?
Windows 服务需要在管理员帐户下运行,有谁知道我如何让安装项目在安装过程中为 Windows 服务设置“用户登录”设置?
At the moment I have to manually right click on the service and set it to log on as the administrator everytime I update the service.
目前,我必须手动右键单击该服务并将其设置为每次更新服务时以管理员身份登录。
Thanks!
谢谢!
采纳答案by Bermo
You should be able to add a new ServiceProcessInstallerin the InitializeComponent()method of your installer. This class will allow you to set account type, username, and password that you want the service to run as. For example:
您应该能够在安装程序的InitializeComponent()方法中添加一个新的ServiceProcessInstaller。此类将允许您设置希望服务运行的帐户类型、用户名和密码。例如:
this.Installers.Add(
new System.ServiceProcess.ServiceProcessInstaller()
{
Account = ServiceAccount.User,
Username = @"domain\username",
Password = "password"
});
If you don't want to hardcode a password into your setup project, then leave it blank and a popup dialog should appear asking for this during install.
如果您不想将密码硬编码到您的安装项目中,请将其留空,并且在安装过程中应该会出现一个弹出对话框,要求您输入密码。