Install Window Service Programmatically

First implement the window service and build it. Your output will be create in the debug/release folder as an exe. Now to install window service on machine, do the following –

Download .cs file from following location
http://dl.dropbox.com/u/5969904/Learnings/ServiceInstaller.cs

Write following code to install the service –

string ServiceName = “TestWindowService”;

string ServiceDisplayName = “TestWindowService”;

string ServiceExePath = @”C:\TestWindowService\Debus\TestWindowService.exe”;

ServiceTools.ServiceInstaller.InstallAndStart(ServiceName, ServiceDisplayName, ServiceExePath);

Write following code to UnInstall the service –

string ServiceName = “TestWindowService”;

//Check if serice is installed

if (ServiceTools.ServiceInstaller.ServiceIsInstalled(ServiceName))

{

//Uninstall Service

ServiceTools.ServiceInstaller.Uninstall(ServiceName);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: