Showing posts with label Windows service. Show all posts
Showing posts with label Windows service. Show all posts

Thursday, February 14, 2008

Windows Service startup path

Ever wondered how to find the startup path for a windows service?
As in windows application,we can never find the startup path of an application using

Application.StartupPath in windows service.

We can find the startup path of a windows service using Reflection.Include the given code in your service and get the startup path returned.

private string StartPath
{
get
{
return System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly()
.GetName().CodeBase).Substring(6);
}
}

Hope this helps..

Monday, February 11, 2008

Creating Windows Service



This example was created soon after I made a windows service as I got to have several doubts myself when creating one. Thought this might be of help to someone or the other.

I have included necessary images for all the stages. Hope that helps.

Starting off. Take new project.


Take windows service and give proper name to it.

Initially service page looks like

Take properties of service page.

Change name and service name to project name

Add eventlog to service page by taking the toolbox and adding eventlog to it.

Now take code and add the following lines of code to the service1.cs page.

now right click on the service design page and add installer to it

Take serviceprocessInstaller1 properties

Change account type to local system or local service

I’ve changed type to local system here.

Now right click on service installer and take properties

Change start type to automatic.

Now the properties page look like,

Now build the project.

On successful build add a setup to the project.

Right click on the setup project and add the project output to it

and click ok.

Now right click on the setup project and take view-> custom actions

Now right click on custom actions and select add custom action.

Double click on application folder and Add primary output.

Click ok.

Now the installer custom actions look like

Now build setup

Now right click on setup and install

Now take services from administrative tools to start the service

From services select our project name and right click that to select start of the service.

Once the dialog box closes the service will be started. To track the service details take server explorer from vs.net

In that take the corresponding source from event logs.It will show our message “Service Started”.

If you right click on the WindowsServiceExample in services and stop it.Then the corresponding message will be shown there in server explorer.

Hope I was clear.So happy programming.