C#写windows服务

来源:互联网 发布:ubuntu什么版本稳定 编辑:程序博客网 时间:2024/05/18 13:09

 1、在VS2010创建windows service工程,文件--新建--项目--windows服务,名称:TestWindowsService,设置server1的ServiceName属性为TestWindowsService

图片

2、由于服务是要安装的,所以它运行的时候就需要一个安装类Installer将服务安装到计算机,新建一个后台服务安装类Install继承自Installer,安 装初始化的时候是以容器进行安装的,所以还要建立ServiceProcessInstaller和ServiceInstaller服务信息组件添加到 容器安装,在Install类增加如下代码:
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.ComponentModel;

namespace TestWindowsService
{
    [RunInstaller(true)]//注意这里的属性Attribute设置
    class Install : Installer
    {
        private System.ComponentModel.IContainer components = null;
        private System.ServiceProcess.ServiceProcessInstaller spInstaller;
        private System.ServiceProcess.ServiceInstaller sInstaller;
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();

            // 创建ServiceProcessInstaller对象和ServiceInstaller对象
            this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.sInstaller = new System.ServiceProcess.ServiceInstaller();

            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.spInstaller.Username = null;
            this.spInstaller.Password = null;

            // 设定服务名称
            this.sInstaller.ServiceName = "TestWindowsService";
            sInstaller.DisplayName = "后台Windows服务";
            sInstaller.Description = "一个后台Windows运行的服务";

            // 设定服务的启动方式
            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });

        }
    }
}

3、点击Service1.cs的视图设计器,在“单击此处切换到代码视图”上右键--添加安装程序,就出现了ProjectInstaller.cs。
点击ProjectInstaller.cs--选择serviceInstaller1--设置ServiceName为TestWindowsService,StartType为Automatic。选择serviceProcessInstaller1--设置account为LocalSystem
点击运行--出现:“无法从命令行或调试器启动服务”
4、一个空的服务基本上搞定了,只是业务没有,就看怎么在服务器上安装部署了.C#写的Windows后台服务不能直接安装,需要借助.NET Framework里面的InstallUtil.exe安装工具安装,我们可以做成一个执行CMD命令的文件BAT文件来安装启动它,命令如下:
%windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe �%\TestWindowsService.exe
图片

安装完成以后,我们可以在我的电脑管理服务里面看到才安装上的后台服务.

图片 

 
5、卸载:找到开始--运行--regedit,找到目录下的TestWindowsService:
 
图片 

图片 

 
删除后重新启动就可以了。
6、C#获取服务程序路径string PathBase = System.AppDomain.CurrentDomain.BaseDirectory;

 
//-------------------另一种安装部署时添加注册表信息实现开机自启动------------------------
使用VS自带的打包模块可以很方便的对项目进行打包部署,同时我们也可以在安装部署时操作注册表实现开机启动软件。具体实现如下:     创建安装部署这部分就不用说了,添加安装部署项目后,鼠标右键安装项目->视图->注册表, 要使软件在开机就运行,可以在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run中 添加键值保存软件目录。在这里我们依次添加以上的项,然后在Run中添加键值,键名可以自己起,value要填软件的物理路径。物理路径是客户在部署确定 的,我们如何获取呢?这里我们可以使用[TARGETDIR]获取客户选择的路径,在加上软件的启动文件名称。比如软件启动文件的名称是 Client.exe,那么Value的值就为:[TARGETDIR]Client.exe.生成安装项目。找到bin目录下的setup.exe文件 运行,安装结束后我们可以在注册表中找到相应的键值。重启电脑系统就会自动运行我们设置的软件。