C#Windows服务程序之安装项目

开发 后端
C#Windows服务程序之安装项目主要向你介绍如何创建Windows服务应用程序(以前称为"NT 服务"的一个安装项目,希望对你了解C#Windows服务程序有所帮助。

C#Windows服务程序之安装项目的由来:本文介绍如何创建Windows 服务应用程序(以前称为"NT 服务")的一个安装项目。 为此,必须首先创建一个解决方案包含简单的 Windows 服务项目,将项写入它的应用程序日志。 然后您将一个安装程序项目添加到解决方案以安装 Window 服务。 ***,您启动该服务。 您可以通过使用开始菜单中的管理工具文件夹中的服务项启动服务。

C#Windows服务程序之为Windows 服务创建安装项目

此部分介绍了如何创建 Windows 服务项目,以及如何使用一个已编译的安装项目若要安装 Windows 服务。

C#Windows服务程序之创建一个 Windows 服务项目

1、启动Microsoft Visual Studio。

2、在 文件 菜单上, 指向 新建 ,然后单击 Project 。

3、在 项目类型 ,下单击 Visual C# 项目 ,然后在 模板 下单击 Windows 服务 。

注意 在 Visual Studio 2005 或 Visual Studio 2008,展开 Visual C# 项目类型 下,单击 Windows ,然后单击 Windows 服务 在 模板 下。

4、类型 LogWriterService 名称 文本框中,然后键入 C:\ 在 位置 文本框中中。 单击 确定 。

5、在解决方案资源管理器, Service1.cs ,右键单击,然后单击 查看代码 。

6、在 OnStart 事件处理程序中, 替换注释以下代码:

EventLog.WriteEntry("My simple service started.");

7、在解决方案资源管理器,双击 Service1.cs 。

8、在代码编辑器窗口,用鼠标右键单击 设计视图 ,然后单击 属性

9、在属性窗格中, 单击 添加安装程序 链接。

10、在为 ServiceInstaller 1 属性窗格,更改 ServiceName 属性,以 Service 1 。

11、在设计视图中代码编辑器窗口,单击 ServiceProcessInstaller 1 。

12、在属性窗格,将 帐户 属性更改为 LocalSystem (: LocalService 和 NetworkService 值都可仅在 Microsoft Windows XP 中获得)。

使用一个已编译的安装程序项目来安装 Windows 服务

在完成上一节来配置 Windows 服务项目中的步骤之后,请按照下列步骤添加部署项目打包服务应用程序以便可以安装服务应用程序的操作:

1、将一个新的项目添加到您的 LogWriterService 项目中。 为此,请按照下列步骤操作:

a、在解决方案资源管理器,右键单击 解决方案 LogWriterService (1 项目) ,指向 添加 ,然后单击 新建项目 。

b、单击 安装和部署项目 在 项目类型 ,然后在 模板 下单击 安装程序项目 。

c、在 名称 文本框中,键入 ServiceSetup 。

d、类型 C:\ 位置 文本中框,然后再单击 确定 。

2、告诉在部署项目的内容到程序包。 为此,请按照下列步骤操作:

a、在解决方案资源管理器,右键单击 ServiceSetup ,指向 添加 ,然后单击 Project Output (项目输出

b、在 添加项目输出组 对话框, 项目 框中的单击 LogWriterService

c、单击 Primary output (主要输出) ,然后单击 确定 。

3、为正确的安装,添加仅主输出。 要添加自定义操作,请按照下列步骤操作:

a、在解决方案资源管理器,右键单击 ServiceSetup ,指向 视图 ,然后单击 自定义操作

b、用鼠标右键单击 自定义操作 ,然后单击 添加自定义操作 。

c、单击 主输出 LogWriterService (Active) ,然后单击 确定 。

您会注意到 主输出 出现在 安装 、 提交 、 回滚 和 卸载 。

4、默认情况下生成配置中不包含安装程序项目。 为构建C#Windows服务程序解决方案,使用下列方法之一:

C#Windows服务程序方法 1

用鼠标右键单击 LogWriterService ,然后单击 生成 。

用鼠标右键单击 ServiceSetup ,然后单击 生成 。

C#Windows服务程序方法 2

在 生成 菜单上, 单击 配置管理器 来构建整个解决方案。

单击以选中 生成 复选框为 ServiceSetup。

按 F 7 键来构建整个解决方案。 生成解决方案时, 必须可用于该服务是完整的安装包。

5、若要安装新建的服务, ServiceSetup ,右键单击,然后单击 安装 。

6、在 ServiceSetup 对话框,单击 下一步 三次。 您会注意到一个进度栏出现服务安装期间。

7、安装服务时, 单击 关闭 。

  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.ServiceProcess;  
  7.  
  8. namespace LogWriterService  
  9. {  
  10. public class Service1 : System.ServiceProcess.ServiceBase  
  11. {  
  12. /// ﹤summary﹥   
  13. /// Required designer variable.  
  14. /// ﹤/summary﹥  
  15. private System.ComponentModel.Container components = null;  
  16.  
  17. public Service1()  
  18. {  
  19. // The Windows.Forms Component Designer must have this call.  
  20. InitializeComponent();  
  21.  
  22. // TODO: Add any initialization after the InitComponent call  
  23. }  
  24.  
  25. // The main entry point for the process  
  26. static void Main()  
  27. {  
  28. System.ServiceProcess.ServiceBase[] ServicesToRun;  
  29.  
  30. // More than one user service may run in the same process. To add  
  31. // another service to this process, change the following line to  
  32. // create a second service object. For example,  
  33. //  
  34. //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};  
  35. //  
  36. ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };  
  37.  
  38. System.ServiceProcess.ServiceBase.Run(ServicesToRun);  
  39. }  
  40.  
  41. /// ﹤summary﹥   
  42. /// Required method for Designer support - do not modify   
  43. /// the contents of this method with the code editor.  
  44. /// ﹤/summary﹥  
  45. private void InitializeComponent()  
  46. {  
  47. components = new System.ComponentModel.Container();  
  48. this.ServiceName = "Service1";  
  49. }  
  50.  
  51. /// ﹤summary﹥  
  52. /// Clean up any resources that are being used.  
  53. /// ﹤/summary﹥  
  54. protected override void Dispose( bool disposing )  
  55. {  
  56. if( disposing )  
  57. {  
  58. if (components != null)   
  59. {  
  60. components.Dispose();  
  61. }  
  62. }  
  63. base.Dispose( disposing );  
  64. }  
  65.  
  66. /// ﹤summary﹥  
  67. /// Set things in motion so your service can do its work.  
  68. /// ﹤/summary﹥  
  69. protected override void OnStart(string[] args)  
  70. {  
  71. EventLog.WriteEntry("My simple service started.");  
  72. }  
  73.  
  74. /// ﹤summary﹥  
  75. /// Stop this service.  
  76. /// ﹤/summary﹥  
  77. protected override void OnStop()  
  78. {  
  79. // TODO: Add code here to perform any tear-down necessary to stop your service.  
  80. }  
  81. }  
  82. }  

C#Windows服务程序验证它正常工作

1、在控制面板,双击 管理工具 ,然后双击 服务

2、用鼠标右键单击 Service 1 ,然后单击 开始

3、使用以下方法之一来验证事件日志中记录一个事件:

C#Windows服务程序验证方法 1

a、在控制面板中, 双击 管理工具 ,然后双击 事件查看器 。

b、在左窗格中, 单击 应用程序日志 ,然后查找从右窗格中您的服务在事件日志中。

C#Windows服务程序验证方法 2

a、在 Server Explorer (服务器资源管理器,) 中展开 服务器 、 ComputerName、 事件日志 、 展开 应用程序 ,然后展开 Service 1 。 请记住 Service 1 是类,非服务的名称本身。 因此, Service 1 用作应用程序的名称。 (它是超出了本文说明了如何自定义名称的范围。

b、日志条目上移动光标。 从顶部第二个条目应阅读"我的简单服务开始"。

C#Windows服务程序之安装项目的基本内容就向你介绍到这里,希望对你了解和学习C#Windows服务程序有所帮助。

【编辑推荐】

  1. C#启动windows服务的方法浅析
  2. C#windows服务状态改变操作浅析
  3. C#Windows服务程序开发实例介绍
  4. C#启动Windows服务及关闭实例实现
  5. C#启动Windows服务的窗体程序浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-14 13:41:13

C#Windows服务

2009-08-14 15:47:18

C#Windows服务

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 15:06:08

Windows服务程序

2009-08-14 14:25:09

Windows服务程序

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 14:45:03

C#Windows服务

2009-08-14 15:54:50

Windows服务程序C#Windows服务

2009-08-14 11:15:19

文件监视C#Windows服务

2009-08-14 10:50:09

Windows服务介绍

2009-08-14 17:27:30

C#Windows应用

2009-08-14 14:53:55

WINDOWS服务交互

2009-08-14 17:43:20

C#Windows应用

2009-08-14 17:55:52

C#Windows应用

2009-08-14 16:13:25

C#windows服务

2009-08-14 18:04:59

C#Windows应用

2009-08-14 17:51:32

C#Windows应用

2009-08-14 17:36:20

C#Windows应用

2009-08-14 10:42:16

Timer控件的使用C#windows服务
点赞
收藏

51CTO技术栈公众号