Is it possible to programmatically set the user account for a windows service?

12,773

Solution 1

Take a look at System.ServiceProcess.ServiceProcessInstaller

Solution 2

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>;
    }

Solution 3

Take a look at DynamicInstaller from CodeProject

Share:
12,773
swolff1978
Author by

swolff1978

Trying to learn best practices and become a more well rounded programmer. Suggestions welcome!

Updated on June 30, 2022

Comments

  • swolff1978
    swolff1978 almost 2 years

    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?

  • Adam Neal
    Adam Neal about 12 years
    Can you use this to set the username & password at runtime (rather than compile-time)? Wouldn't you need to be able to get to the installers that are already compiled into your service executable? I'll probably have to go the WMI route instead.
  • MattH
    MattH about 12 years
    Your service can have its own installer class that inherits from "System.Configuration.Install.Installer". We use this inherited class and associate a form with it. The form comes up during the install and prompts for various things, including user/password. Then in the Installer class you override OnBeforeInstall and set Me.ServiceProcessInstaller1.Username.
  • Kiquenet
    Kiquenet almost 10 years
    And modify windows service account for WinService that yet exists?
  • Viswa
    Viswa almost 9 years
    If you do not specify an Account in code and have the ServiceProcessInstaller Property for Account set to User a form dialog will open during the installation that will allow the individual who performs the installation to specify a domain user account and password.
  • Grubsnik
    Grubsnik over 8 years
    shameless plug for something that isn't around any longer.