VS2008 Windows service

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值