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

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?

Answers


Take a look at System.ServiceProcess.ServiceProcessInstaller


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

Take a look at DynamicInstaller from CodeProject


There is a bit about setting service parameters and stuff in A Windows Service without a template Its on page 5 in the bit about customising a service.


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.

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

</ShamelessPlug>

Cheers!


Need Your Help

What does $VARIABLE$ stand for

bash nagios

..in the following shell script?