Welcome to MSDN Blogs Sign in | Join | Help

How to Install Windows Service Programmatically

Sometimes you may want to install a Windows Service programmatically, but the target machine does not have InstallUtil.exe.

To install a Windows Service programmatically, you can build an application to install that Windows Service.

  • Add a reference to System.Configuration.Install
  • Use this code:
public static void InstallService(string ExeFilename)
{
    System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename);
    Installer.UseNewContext = true;
    Installer.Install(null);
    Installer.Commit(null);
}

To uninstall:

public static void UninstallService(string ExeFilename)
{
    System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename);
    Installer.UseNewContext = true;
    Installer.Uninstall(null);
}
Published Tuesday, October 21, 2008 12:41 AM by HelloWorld

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Reflective Perspective - Chris Alcock » The Morning Brew #205

# Interesting Finds: 2008.10.19~2008.10.23

.NET ASP.NET MVC Beta Source Code Release - Partial Output Caching in ASP.NET MVC - Partial Requests

Thursday, October 23, 2008 8:12 PM by gOODiDEA.NET

# How to Create Custom Event Log for Windows Service

When you created a Windows Service, you usually add a Service Installer to allow this service to be installed

Thursday, December 11, 2008 1:43 PM by You had me at "Hello World"

# re: How to Install Windows Service Programmatically

It didn't worked for me :(

When I call the install method, a black screen freezes saying:

Installing assembly x

Affected parameters are:

assemblypath = y

logFile = z

And the log file has the same text

Monday, June 08, 2009 2:20 PM by Hugo

# re: How to Install Windows Service Programmatically

Hi,

It is working fine. Can you explain how to install the same service in the remote system.

Thanks,

Sams

Thursday, June 11, 2009 10:48 PM by shivaadabala

# re: How to Install Windows Service Programmatically

@Hugo: I do not have enough information from your error message. Is this a service application?

@Sams: Sorry, I have not done remote install yet.

Tuesday, June 30, 2009 3:22 PM by HelloWorld

# re: How to Install Windows Service Programmatically

I tried running this via ASP.NET and got the following error:

The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

I found a comment in the following which basically said needs to be run as administrator:

http://msdn.microsoft.com/en-us/library/50614e95(VS.80).aspx

Have you come across this problem and do you know a way to resolve it?

Regards,

John

Saturday, August 22, 2009 3:47 AM by John

# re: How to Install Windows Service Programmatically

John,

You need to be an administrator to install a service.

Friday, August 28, 2009 1:33 AM by HelloWorld

# re: How to Install Windows Service Programmatically

Thursday, November 05, 2009 10:09 AM by dizzy

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker