Operating System, Software, Visual Studio, Windows

Create a Windows Service with Visual Studio

create-a-windows-service-with-visual-studio

When you would like to start a program, users always copy the file or link the shortcut of file to the autorun directory.
In Windows, the autorun directory is located at Start Menu > All Programs > Startup

However, different users have different settings, so if another user login the Windows System, he/she will not trigger the same startup file.
Windows Systems perform some background tasks when they are booted.
Some of these tasks start before users logging in, we called them Windows Service

We can write our own Windows Service, for example auto logout when time is out.
(This service always performs in public computer like public library, but I unrecognized it must a Windows Service)

In generally, you should install the Visual Studio before.

Create a Windows Service with Visual Studio
Click File > New > Project to open a new project

Create a Windows Service with Visual Studio
Expand Visual C# (you can use other language, but this time we use C#)
And select Windows Service Visual C#
Then set the project name to Name field and click OK to start the project

Create a Windows Service with Visual Studio
Visual Studio displays like this
If the Service1.cs [Design] disappeared, double click the Service1.cs in the Solution Explorer

Create a Windows Service with Visual Studio
Click click here to switch to code view on the Service1.cs
Then change to code view of Service1.cs
There are two blanked method generated automatically by Visual Studio

protected override void OnStart(string[] args)

This method will run when this service is started

protected override void OnStop()

The method will run then this service is stopped
You also can override more methods, you can find them in MSDN
These statements are presenting a test run only, you can change them to your aim or needed
However, you cannot use any GUI libraries in Windows Service

Create a Windows Service with Visual Studio
Back to the Service1.cs [Design]
Right click any blank area in the design form then click Add Installer

Create a Windows Service with Visual Studio
Create a ProjectInstaller.cs [Design]
Click the serviceInstaller1 then right click it and click Properties (If you expand the properties window, you don’t need to to it)
Modify ServiceName And StartType for your service (I suggest you don’t use special character including <space>)
Automatic means when you booted the system, the service will start automaticlly
Manual means you should start the service manually
Disabled means you should enable the service before you would to start it

Create a Windows Service with Visual Studio
Click the serviceProcessInstaller1 then right click it and click Properties (If you expand the properties window, you don’t need to to it)
Modify account to LocalSystem

Create a Windows Service with Visual Studio
Right click your service project and click Properties

Create a Windows Service with Visual Studio
On the properties dialog, change Startup object to your service program

Create a Windows Service with Visual Studio
Right click your solution Add > New Project

Create a Windows Service with Visual Studio
Expand Other Object Type > Setup and Development, click Setup Project
And name the setup project in Name field
Then click OK

Create a Windows Service with Visual Studio
Right click your setup project Add > Project Output

Create a Windows Service with Visual Studio
Select your service project in Project and select Primary
And (Active) in Configure
Then click OK

Create a Windows Service with Visual Studio
Right click your setup project View > Custom Actions

Create a Windows Service with Visual Studio
In Custom Actions form, right click Custom Actions and click Add Custom Action

Create a Windows Service with Visual Studio
Double click the Application Folder

Create a Windows Service with Visual Studio
Select Project Output from <your service project> (Active) then click OK

Create a Windows Service with Visual Studio
After you finished the actions before, the form presents like this

Create a Windows Service with Visual Studio
Right click your service project and click build
If you have built the service, you should click rebuild

Create a Windows Service with Visual Studio
Right click your setup project and click build
If you have built the setup, you should click rebuild

Create a Windows Service with Visual Studio
Access to the setup project directory the setup.exe file is located at debug
directory

Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Install the service

Create a Windows Service with Visual Studio
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process

Create a Windows Service with Visual Studio
Open the command line mode

Create a Windows Service with Visual Studio
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the installation process

Create a Windows Service with Visual Studio
type

net start "<your service name>"

to start your service
You also can start your service in Control Panel > Administrative Tools > Services

Create a Windows Service with Visual Studio
The result represent the action when started this service

Create a Windows Service with Visual Studio
type

net stop "<your service name>"

to start your service
You also can stop your service in Control Panel > Administrative Tools > Services

Create a Windows Service with Visual Studio
The result represent the action when stopped this service

Create a Windows Service with Visual Studio
Goto Control Panel > Uninstall a Program

Create a Windows Service with Visual Studio
Find out your service setup

Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Create a Windows Service with Visual Studio
Click Uninstall at the center to begin the uninstall wizard

Create a Windows Service with Visual Studio
If you are using Windows VISTA, Windows 7, Windows Server 2008, you are able to use administrator permission to allow the uninstallation process