是否可以以编程方式设置 Windows 服务的用户帐户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1234055/
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
Is it possible to programmatically set the user account for a windows service?
提问by swolff1978
I have created a windows service that has the Account set to user. Which means that when I install the service I need to pass a user name and password. Is there a way to set these maybe in the ProjectInstaller class maybe in the BeforeInstall event? if so HOW?
我创建了一个将帐户设置为用户的 Windows 服务。这意味着当我安装服务时,我需要传递用户名和密码。有没有办法在 ProjectInstaller 类中可能在 BeforeInstall 事件中设置这些?如果是这样怎么办?
采纳答案by MattH
回答by Chill
The below addition to a project installer will assign the service Log On information during installation.
以下添加到项目安装程序将在安装期间分配服务登录信息。
public ProjectInstaller()
{
InitializeComponent();
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.<account type>;
serviceProcessInstaller1.Username = <domain\userId>;
serviceProcessInstaller1.Password = <password>;
}
回答by Shay Erlichmen
Take a look at DynamicInstaller from CodeProject
回答by Shay Erlichmen
There is a bit about setting service parameters and stuff in A Windows Service without a templateIts on page 5 in the bit about customising a service.
在没有模板的 Windows 服务中,有一些关于设置服务参数和内容的内容,它在第 5 页的关于自定义服务的部分中。
回答by jake.stateresa
Normally you will be able to pass those credentials to the Installer class. You can either hard-code it or pass it as a command-line argument. The second approach is more appropriate but it will require you to parse the command-line arguments unnecessarily.
通常,您可以将这些凭据传递给 Installer 类。您可以对其进行硬编码或将其作为命令行参数传递。第二种方法更合适,但它需要您不必要地解析命令行参数。
I propose to you a third approach...
我向你建议第三种方法......
<ShamelessPlug>
<无耻插件>
Hi! I am a developer for an open source windows service hosting framework called Daemoniq. And passing credentials via command-line is one of its features. You can download it from http://daemoniq.org
Current features include:
- container agnostic service location via the CommonServiceLocator
- set common service properties like serviceName, displayName, description and serviceStartMode via app.config
- run multiple windows services on the same process
- set recovery options via app.config
- set services depended on via app.config
- set service process credentials via command-line
- install, uninstall, debug services via command-line
你好!我是一个名为 Daemoniq 的开源 Windows 服务托管框架的开发人员。通过命令行传递凭据是其功能之一。你可以从http://daemoniq.org下载
当前功能包括:
- 通过 CommonServiceLocator 的容器不可知服务位置
- 通过 app.config 设置公共服务属性,如 serviceName、displayName、description 和 serviceStartMode
- 在同一进程上运行多个 Windows 服务
- 通过 app.config 设置恢复选项
- 通过 app.config 设置依赖的服务
- 通过命令行设置服务进程凭据
- 通过命令行安装、卸载、调试服务
</ShamelessPlug>
</无耻插件>
Cheers!
干杯!