Create Installer For Windows Application

Posted on by

IN this post, we go over how to get started using C# and .NET by creating a basic Windows service with these technologies.

Join the DZone community and get the full member experience.

Now your application needs a connection to database to run properly. Proceed to the Set Up the Database Connection lesson, to learn how to do it. To familiarize yourself with alternative deployment methods, take one of the following lessons. Xcopy Deployment of a Windows Forms Application; Publish Wizard Deployment of a Windows Forms Application. Create installer windows application visual studio 2010 create installer windows application create installer for windows application visual studio 2013 create installer for windows application.

Join For Free

Here I’m going to explain Windows Services in C# .NET.

  • Introduction of Windows Services.
  • How to create Windows Services in C# .NET.

Introduction

Windows Services normally start when the OS boots and runs an application in the background. Windows Services executes applications in its own session. It either starts automatically or we can manually pause, stop, and restart it.

You can find services in the following ways

  • Go to Control Panel select “Services” inside “Administrative Tools.”
  • Open Run window (Window + R), type in services.msc, and press enter.

How to Create a Windows Service

Step 1

Create Installer For Windows Program

Open Visual Studio, go to File > New and select Project. Now select a new project from the Dialog box and select “Window Service” and click on the OK button.

Step 2

Go to Visual C# -> ”Windows Desktop” -> ”Windows Service,” give your project an appropriate name and then click OK

Once you click OK, the below screen will appear, which is your service

Step 3

Right-click on the blank area and select “Add Installer.”

Adding Installers to the Service

Before you can run a Windows Service, you need to install the Installer, which registers it with the Service Control Manager.

After Adding Installer, ProjectInstaller will add in your project and ProjectInstakker.cs file will be open. Don’t forget to save everything (by pressing ctrl + shift + s key)

The Solution Explorer looks like this:

Step 4

Right click on blank area and select “View Code”

Step 5

Its has a Constructor, which contains the InitializeComponentmethod.

The InitializeComponentmethod contains the logic which creates and initializes the user interface objects dragged on the form's surface and provides the Property Grid of Form Designer.

Very important: Don't ever try to call any method before the call of InitializeComponentmethod.

Step 6

Select the InitializeComponentmethod and press the F12 key to go the definition.

Create Installer For Windows Application Visual Studio 2017

Step 7

Now add the below line while installing the service:

You also can add a description and display the service name (optionally).

Step 8

In this step, we will implement a timer, and code to call a service at a given time. We will create a simple write in a text file.

Service1.cs class

Code explanation - the above code will call a service every 5 seconds and create a folder if none exist and write our message.

Step 9: Rebuild Your Application

Right-click on your project or solution and select Rebuild.


Step 10

Search “command Prompt” and run the program as an administrator:

Step 11

Fire the below command in the command prompt and press Enter.

cd C:WindowsMicrosoft.NETFrameworkv4.0.30319

Step 12

Now Go to your project source folder > bin > Debug and copy the full path of of the Windows Service.exe file

Step 13

Open the command prompt and fire the below command and press enter.

Syntax

InstallUtil.exe + Your copied path + your service name + .exe

Our Path

InstallUtil.exe C:UsersFaisal-PathansourcereposMyFirstServiceMyFirstServicebinDebugMyFirstService.exe

Windows

Step 14

Open services by following the below steps:

  1. Press Window key + R.
  2. Type services.msc
  3. Find your Service.

Service Output

A log folder will be created in your bin folder.

If you want to uninstall the service, fire the below command.

  1. Syntax InstallUtil.exe -u + Your copied path + your service name + .exe
  2. Our path InstallUtil.exe -u C:UsersFaisal-PathansourcereposMyFirstServiceMyFirstServicebinDebugMyFirstService.exe

Summary

In this article, we learned how to create a Windows Service and install/uninstall it using the InstallUtil.exe from a command prompt.

I hope that I have explained each step clearly and that it can be easily understood by all developers. Free iso 14971 standard pdf. You can leave feedback/comments/questions to this article. Please let me know if you like and understand this article and how I could improve it.

I also uploaded this project on GitHub, here.

If you enjoyed this post, Faisal has also written a great article on C# Best Practices.

Like This Article? Read More From DZone

c# ,.net ,web dev ,.net development

Published at DZone with permission of Faisal Pathan . See the original article here.

Create Installer Windows Form

Opinions expressed by DZone contributors are their own.