Académique Documents
Professionnel Documents
Culture Documents
Sign in
articles
quick answers
discussions
community
help
CPOL
Rate this:
4.15 (4 votes)
Creating a self installing Windows service with C#
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.
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
Julian Ohrt
United States
No Biography provided
AngleSharp
SAPrefs - Netscape-like
Preferences Dialog
Layout Normal
Go
Per page 50
Update