c#Windows服务

1.vs ->Windows服务



2.创建完成后在项目下默认会创建 Program.cs 和 Service1.cs

双击Service1.cs到设计,在设计区右击,选择添加安装程序

产生2个组建

点击ServiceInstaller1 设置相关属性

3.在Service1.cs 事件:

 protected override void OnStart(string[] args)        {        }服务开启时
 protected override void OnStop()        {        }服务停止时

要想在程序安装时自动启动服务:

在ProjectInstaller.cs里:

public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstall_Committed);
}
private void ProjectInstall_Committed(object obj, InstallEventArgs eventArgs)
{
System.ServiceProcess.ServiceController controller
= new ServiceController("服务名");
controller.Start();
}

4.编译后在Debug里的.exe 不是双击执行,要在C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe 来执行编译后的exe文件

posted @ 2011-03-23 12:05  Wythe  阅读(309)  评论(0编辑  收藏  举报