windows-services - 安装 Windows 服务时,如何以编程方式使其使用特定的域帐户

标签 windows-services installation service

我有一个运行良好的 Windows 服务,但我必须让它在特殊用户帐户下运行。

目前,我进入服务并更改登录方式部分,但对于部署,这必须更专业地完成。

有没有办法让我以编程方式或在服务安装过程中以自定义用户帐户身份登录?

最佳答案

当您打开服务控制管理器(SCM)时,当然会有一个标记为“登录”的选项卡。您可以在其中指定它应在哪个域或计算机帐户下运行...

但是以编程方式。如果您在代码中使用服务安装程序类,您可以在那里指定它..

 public class MyServiceInstaller : Installer
    {
        private ServiceInstaller servInst;
        private ServiceProcessInstaller servProcInst;
        public MyServiceInstaller () { InitializeComponent(); }

        #region Component Designer generated code
        private void InitializeComponent()
        {
            servInst = new ServiceInstaller();
            servProcInst = new ServiceProcessInstaller();
            // -----------------------------------------------
            servProcInst.Account = ServiceAccount.LocalSystem; // or whatever accnt you want
            servProcInst.Username = null;  // or, specify a specifc acct here
            servProcInst.Password = null;
            servProcInst.AfterInstall += 
                new InstallEventHandler(this.AfterServProcInstall);
            servInst.ServiceName = "MyService";
            servInst.DisplayName = "Display name for MyService";
            servInst.Description = " Description for my service";
            servInst.StartType = ServiceStartMode.Automatic;
            servInst.AfterInstall += 
               new InstallEventHandler(this.AfterServiceInstall);
            Installers.AddRange(new Installer[] { servProcInst, servInst });
        }
        #endregion
    }
    private void AfterServiceInstall(object sender, InstallEventArgs e) { }
    private void AfterServProcInstall(object sender, InstallEventArgs e) { }

关于windows-services - 安装 Windows 服务时,如何以编程方式使其使用特定的域帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/378030/

相关文章:

java - 通过 Windows 服务启动 Jar 时 JFrame 未加载

Python Windows 7 - 安装失败 0x80240017

Python3 : ImportError: No module named '_ctypes' when using Value from module multiprocessing

Grails-grails中不同的依赖注入(inject)方式

c# - 在不使用来自另一个应用程序的代码的情况下调试 Windows 服务

c# - 在 Windows 服务中获取当前用户名

c++ - 我们如何找到正在运行的 Windows 服务的进程 ID?

c# - 我需要帮助了解 C# 项目在 Visual Studio 中的组织方式

linux - 如何将 Systemd 服务标准输出从 ExecStart 传输到 shell,例如。 bash ?

linux - icinga2 监控正在运行的 linux 服务