---
title: "VS2008 Windows service"
author: "aaron_ch"
date: "2009-03-18T19:22:10Z"
tags: []
slug: "vs2008-windows-service"
draft: false
lastmod: "2009-03-18T19:24:54Z"
id: 351100
url: "https://aaron-ch.iteye.com/blog/351100"
---

Study the VS2008 recently, find that it's easy to create the windows service by vs2008, and list the steps as below.

 

1.create a console project, and change the program.cs file to below also need add the **System.Configuration.Install,System.ServiceProcess** to reference

  

```
 class Program : ServiceBase
    {
        
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }

        public Program()
        {
            this.ServiceName = "MQ Service";
        }

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Thread.Sleep(10000);
            sendmail();
            //TODO: place your start code here
        }

        protected override void OnStop()
        {
            base.OnStop();

            //TODO: clean up any variables and stop any threads
        }
        public void sendmail()
        {
            try
            {
                EmailCls ec = new EmailCls("smtp.163.com", "uid", "password");
                ec.From = "aaron_ch@163.com";
                ec.To = "xjfaaron@yahoo.com";
                ec.Subject = "test";
                ec.Body = "test body time:" + DateTime.Now.ToLongTimeString();
                //ec.Attachment = Attachment;
                ec.Send();
            }
            catch (Exception ex) { }
        }
    }
```

 2. Create the installer package,code as below

```
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.ComponentModel;
using System.ServiceProcess;

namespace WinService
{
    [RunInstaller(true)]
    public class WinServiceInstaller : Installer
    {
        public WinServiceInstaller()
        {
            var processInstaller = new ServiceProcessInstaller();
            var serviceInstaller = new ServiceInstaller();

            //set the privileges
            processInstaller.Account = ServiceAccount.LocalSystem;

            serviceInstaller.DisplayName = "MQ Service";
            serviceInstaller.StartType = ServiceStartMode.Manual;

            //must be the same as what was set in Program's constructor
            serviceInstaller.ServiceName = "MQ Service";
            serviceInstaller.Description = "Customized MQ Service for Recieve Message from MSMQ";

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}
```

 

3.Build the project, then copy the WinService.exe from bin folder, to C:\\Program Files\\Microsoft Visual Studio 9.0\\VC  folder,

3.1 install the service to service list by: run the cmd in vs2008 cmd tools 

**installutil WinService.exe**

**3.2** Uninstall the service from service list by **installutil -u WinService.exe**

Souce code see attached!