C#创建windows服务并发布

创建window 服务

新建一个window 服务项目MyService,如下图

切换到代码视图修改.

[csharp] view plaincopy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.ServiceProcess;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MyService
  12. {
  13. public partial class Service1 : ServiceBase
  14. {
  15. //定时器
  16. System.Timers.Timer t = null;
  17. public Service1()
  18. {
  19. InitializeComponent();
  20. //启用暂停恢复
  21. base.CanPauseAndContinue = true;
  22. //每5秒执行一次
  23. t = new System.Timers.Timer(5000);
  24. //设置是执行一次(false)还是一直执行(true);
  25. t.AutoReset = true;
  26. //是否执行System.Timers.Timer.Elapsed事件;
  27. t.Enabled = true;
  28. //到达时间的时候执行事件(theout方法);
  29. t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
  30. }
  31. //启动服务执行
  32. protected override void OnStart(string[] args)
  33. {
  34. string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "启动";
  35. WriteLog(state);
  36. }
  37. //停止服务执行
  38. protected override void OnStop()
  39. {
  40. string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "停止";
  41. WriteLog(state);
  42. }
  43. //恢复服务执行
  44. protected override void OnContinue()
  45. {
  46. string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "继续";
  47. WriteLog(state);
  48. t.Start();
  49. }
  50. //暂停服务执行
  51. protected override void OnPause()
  52. {
  53. string state = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "暂停";
  54. WriteLog(state);
  55. t.Stop();
  56. }
  57. public void WriteLog(string str)
  58. {
  59. using (StreamWriter sw = File.AppendText(@"d:\service.txt"))
  60. {
  61. sw.WriteLine(str);
  62. sw.Flush();
  63. }
  64. }
  65. public void theout(object source, System.Timers.ElapsedEventArgs e)
  66. {
  67. WriteLog("theout:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
  68. }
  69. }
  70. }

解释:OnStart和OnStop分别是服务器启动和停止后,所发生的事件操作方法.定义了一个定时器,每隔5秒执行一次(theout方法),因为暂停恢复功能默认是不启用的,需要设置CanPauseAndContinue属性启用此功能,同时重写OnStop和OnContinue方法,添加自己的逻辑代码.

将服务程序service1.cs 切换到视图模式,用鼠标右键单击设计视图选择“添加安装程序”选项,此后在项目中自动增加了一个ProjectInstaller.cs,

如下图

打开ProjectInstaller,修改serviceInstaller1组件属性

Description= 我的服务备注                       服务备注说明

DisplayName=我的服务                            服务友好名字

ServiceName=MyService                         安装服务器名字

StartType=Automatic                                服务类型

ü         Manual      服务安装后,必须手动启动。

ü         Automatic    每次计算机重新启动时,服务都会自动启动。

ü         Disabled     服务无法启动。

并设计serviceProcessInstaller1的属性Account=LocalSystem

运行编译,一个简单的windows服务已经开发完成.

安装window服务

安装命令:InstallUtil.exe MyServiceLog.exe

InstallUtil存在路径为:C:\WINDOWS\Microsoft.NET\Framework\.NET版本号

复制C:\WINDOWS\Microsoft.NET\Framework\版本号  路径中的InstallUtil.exe 到bin/debug或bin/release文件夹中,在命令行窗口中直接运行命令

InstallUtil.exe MyServiceLog.exe,在系统中注册这个服务,使它建立一个合适的注册项,如下图:

然后再window服务列表中,启动MyServiceLog服务

卸载window 服务

命令:InstallUtil.exe MyServiceLog.exe /u

如果修改这个服务,但是路径没有变化的话是不需要重新注册服务的,直接停止服务,然后用新的文件覆盖原来的文件即可,如果路径发生变化,应该先卸载这个服务,然后重新安装这个服务。

原文地址:https://www.cnblogs.com/taleche/p/9662433.html

时间: 2024-08-29 23:02:37

C#创建windows服务并发布的相关文章

C#——》创建Windows服务,发布并调试Windows服务

一,创建一个windows服务项目.  二,双击Service1.cs进入设计界面,在空白处右键单击选择添加安装程序,如下图所示. 三,添加安装程序后,会进入如下图界面,生成两个组件:serviceProcessInstaller1.serviceInstaller1. 四,设置组件 serviceInstaller1组件的主要属性有: ServiceName: 服务名称,在启动/关闭服务时会需要用到这个属性,用来唯一标识一个服务. StartType:设置为Manual(手动启动),默认停止,

在64位windows下使用instsrv.exe和srvany.exe创建windows服务

在64位windows下使用instsrv.exe和srvany.exe创建windows服务 在32位的windows下,包括windows7,windows xp以及windows 2003,都可以使用instsrv.exe和srvany.exe来创建自定义的windows服务.比如,我们有一个bat文件,用于将指定的程序作为服务进行启动,使用一般的工具都不可以进行此类工作,而使用由windows 2003的资源工具包windows toolkit中所带的instsrv就可以. 详细的用法这

C#创建windows服务、调试、安装

 一.创建Windows服务  二.调试windows程序  三.发布windows程序 如果安装有问题可以使用下面工具进行安装 window服务安装工具下载 示例源码

.NET Core 创建Windows服务

.NET Core 创建Windows服务 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10850564.html 写在前面 使用 TopShelf+Autofac+AutoMapper+Quartz+NLog 完成现有项目定时调度任务 1.相关NetGet包 依赖注入 Alexinea.Autofac.Extensions.DependencyInjection 对象映射 AutoMapper.Extensions.Microsoft.Depend

使用.NET Core创建Windows服务(一) - 使用官方推荐方式

原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 作者:Dotnet Core Tutorials 译者:Lamond Lu 译文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 创建Windows服务来运行批处理任务或者运行后台任务,是一种非常常见的模式,但是由于云服务(Amazon Lambda, Azure WebJobs以及Azure Functions)的

使用.NET Core创建Windows服务(二) - 使用Topshelf方式

原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tutorials 译者:Lamond Lu 译文:使用.NET Core创建Windows服务(二) - 使用Topshelf方式 使用.NET Core创建Windows服务 使用微软推荐方式 使用Topshelf方式 在前一篇文章中,我给大家介绍了,如何基于微软推荐方式使用.NET Core创建Windows

Topshelf创建Windows服务

使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 引用安装 1.官网:http://topshelf-project.c

使用Topshelf 5步创建Windows 服务 z

使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 1.Topshelf的代码托管在http://topshelf-project.c

(转)创建Windows服务(Windows Services)N种方式总结

转自:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html 最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshelfc.利用小工具instsrv和srvany 下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志: a.利用.net框架类