Vous êtes sur la page 1sur 4

12,573,181 members (274,474 online)

Sign in

Search for articles, questions, tips

articles

quick answers

discussions

community

help

Articles Languages C# Windows Services

C# Windows Service with Command Line Installer


and GUI
Julian Ohrt, 9 Nov 2015

CPOL

Rate this:

4.15 (4 votes)
Creating a self installing Windows service with C#

Download source - 25.3 KB

Introduction
There are a lot of tutorials out there explaining how to create a Windows service using C#. Virtually all of them want
you to use a command-line utility called InstallUtil.exe to install the service. But what if you want to install the service
on a machine where this tool is not available? And why should I use it in the first place? It is so easy to create a service
which can install und uninstall itself using command line arguments or even a GUI. The snippets are available in
different places. Let's put them together!

Background
For parsing command line arguments, I use my library CLAReloaded.
The attached code contains a sample service which writes every 30 seconds into a log file. It is a common example. A
large portion is created by Visual Studio including the actual installer object. For details, refer to this tutorial.

Using the Code


Having a service (including installer), you first need to set its name, friendly name, and description. They will be later
visible in the Services manager. In the attached sample, they are attributes of serviceInstaller which in turn is a
child of SampleServiceInstaller:

Then, it can be installed/uninstalled by these four lines:


Hide Copy Code

TransactedInstaller installer = new TransactedInstaller();


installer.Context = new InstallContext(null, null);
installer.Context.Parameters["assemblyPath"] = Assembly.GetExecutingAssembly().Location;
installer.Install(new Hashtable());
//installer.Uninstall(null);
So why should one use InstallUtil.exe? I really cannot tell...

Points of Interest
This interesting detail here is that the only reference to the service passed to the installer is the location of the service
on the hard disk. No need to pass the name or an instance of the service class. This is possible because the program
can distinguish if it is called as program or service. This is done by:
Hide Copy Code

if (Environment.UserInteractive) {
//run as programm
} else {
//run as service
}
You can set some parameters to configure the installation procedure. They are basically identical to the command line
arguments which can be passed to InstallUtil.exe. For example, add thefollowing lines, before calling the actuall
install/uninstall method:
Hide Copy Code

installer.Context.Parameters["logFile"] = "installer_logfile.log";
installer.Context.Parameters["logToConsole"] = "false";
installer.Context.Parameters["showCallStack"] = "";
For launching a GUI form from a console application, all you need to do is:
Hide Copy Code

Application.EnableVisualStyles();
Application.Run(new SampleForm());
If you encounter an error 5 "Access denied" during starting the service, make sure that the file is readable and
executable by user profile SYSTEM.

If installing/uninstalling fails, make sure you are running WindowsService1.exe with elevated rights.

History
2015-11-09: Initial release

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share
EMAIL

TWITTER

About the Author

Julian Ohrt
United States
No Biography provided

You may also be interested in...


Easy Command Line
Service

Generate and add keyword


variations using AdWords
API

Windows Services Can


Install Themselves

Window Tabs (WndTabs)


Add-In for DevStudio

AngleSharp

SAPrefs - Netscape-like
Preferences Dialog

Comments and Discussions


You must Sign In to use this message board.
Search Comments
Spacing Relaxed

Layout Normal

Go

Per page 50

Update

-- There are no messages in this forum --

Permalink | Advertise | Privacy | Terms of Use | Mobile


Web02 | 2.8.161101.1 | Last Updated 9 Nov 2015

Layout: fixed | fluid

Article Copyright 2015 by Julian Ohrt


Everything else Copyright CodeProject, 1999-2016

Vous aimerez peut-être aussi