运行Visual Studio.NET,建立一个C#的Windows服务项目
       项目名称为:TestService
        代码:
C# 创建Windows 服务简单实例using System;
C# 创建Windows 服务简单实例
using System.Collections.Generic;
C# 创建Windows 服务简单实例
using System.ComponentModel;
C# 创建Windows 服务简单实例
using System.Data;
C# 创建Windows 服务简单实例
using System.Diagnostics;
C# 创建Windows 服务简单实例
using System.ServiceProcess;
C# 创建Windows 服务简单实例
using System.Text;
C# 创建Windows 服务简单实例
using System.Threading;
C# 创建Windows 服务简单实例
using System.Windows.Forms;
C# 创建Windows 服务简单实例
namespace TestService
 添加服务安装程序  
  创建一个Windows服务,仅用InstallUtil程序去安装这个服务是不够的。你必须还要把一个服务安装程序添加到你的Windows服务当中,这样便于InstallUtil或是任何别的安装程序知道应用你服务的是怎样的配置设置。  
   
  1.   将这个服务程序切换到设计视图  
  2.   右击设计视图选择“添加安装程序”  
  3.   切换到刚被添加的ProjectInstaller的设计视图  
  4.   设置serviceInstaller1组件的属性:    
          1)   ServiceName   =   My   Sample   Service  
          2)   StartType   =   Automatic  
  5.   设置serviceProcessInstaller1组件的属性    
          1)   Account   =   LocalSystem  
  6.   生成解决方案  
   
  在完成上面的几个步骤之后,会自动由Visual   Studio产生下面的源代码,它包含于ProjectInstaller.cs这个源文件内。
 安装windows 服务:
    这时就是要把个服务安装一下。进入cmd中的画面,进入Framework2.0的文件如:

  cd C:WINDOWSMicrosoft.NETFrameworkv2.0.50727

  后在打

  InstallUtil f:w2.exe 这个就安装了服务 卸载服务是 InstallUtil f:w2.exe -u

  现在就剩启动服务了,

  到windows服务里启动你安装的服务就可以了。

相关文章: