创建一个Windows Service 程序

1.新建Windows项目,选择"Windows服务"类型的项目。

2.在生成的Service1.cs中代码中写你需要的代码,如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.IO;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        Timer timer; 
        
        public Service1() { InitializeComponent(); }

        protected override void OnStart(string[] args)
        {
            timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        protected override void OnStop()
        {
            timer.Stop();
            timer.Dispose();
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            StreamWriter sw = null;
            if (!File.Exists(filePath))
            {
                sw = File.CreateText(filePath);
            }
            else
            {
                sw = File.AppendText(filePath);
            }
            sw.Write("访问时间:" + DateTime.Now.ToString() + Environment.NewLine); 
            sw.Close();
        }
    }
}

 

3.在和Service1.cs这个项目下在新建一个安装程序类Installer1.cs,代码如下:

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace WindowsService1
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        private System.ServiceProcess.ServiceProcessInstaller spInstaller;
        private System.ServiceProcess.ServiceInstaller sInstaller;

        public Installer1()
        {
            InitializeComponent();

            // 创建ServiceProcessInstaller对象和ServiceInstaller对象            
            this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.sInstaller = new System.ServiceProcess.ServiceInstaller();
            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息            
            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.spInstaller.Password = null;
            this.spInstaller.Username = null;
            // 设定服务的名称            
            this.sInstaller.ServiceName = "MyTestWindowsService1";
            //设定服务启动的方式            
            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
        }
    }
}

 

4.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,对应的目录为:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后执行InstallUtil.exe+待执行的exe文件的目录,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。

打开Windows服务列表方式:运行 --> 输入services.msc 

在列表中查找一个叫MyTestWindowsService1 的服务,这个就是你创建安装的服务

 

5.启动该服务,这时打开bin\Debug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。

 

6.卸载服务的操作也和简单,打开命令行工具,转到C:\Windows\Microsoft.NET\Framework\v4.0.30319目录,然后执行InstallUtil.exe -u F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe命令就可以了。

posted @ 2011-11-09 10:45  PPDev  阅读(3581)  评论(1编辑  收藏  举报