C#Windows服务程序中如何为Windows服务创建安装项目

小编给大家分享一下C#Windows服务程序中如何为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、安装服务时, 单击 关闭 。

using System;  using System.Collections;  using System.ComponentModel;  using System.Data;  using System.Diagnostics;  using System.ServiceProcess;   namespace LogWriterService  {  public class Service1 : System.ServiceProcess.ServiceBase  {  /// ﹤summary﹥   /// Required designer variable.  /// ﹤/summary﹥  private System.ComponentModel.Container components = null;   public Service1()  {  // The Windows.Forms Component Designer must have this call.  InitializeComponent();   // TODO: Add any initialization after the InitComponent call  }   // The main entry point for the process  static void Main()  {  System.ServiceProcess.ServiceBase[] ServicesToRun;   // More than one user service may run in the same process. To add  // another service to this process, change the following line to  // create a second service object. For example,  //  //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};  //  ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };   System.ServiceProcess.ServiceBase.Run(ServicesToRun);  }   /// ﹤summary﹥   /// Required method for Designer support - do not modify   /// the contents of this method with the code editor.  /// ﹤/summary﹥  private void InitializeComponent()  {  components = new System.ComponentModel.Container();  this.ServiceName = "Service1";  }   /// ﹤summary﹥  /// Clean up any resources that are being used.  /// ﹤/summary﹥  protected override void Dispose( bool disposing )  {  if( disposing )  {  if (components != null)   {  components.Dispose();  }  }  base.Dispose( disposing );  }   /// ﹤summary﹥  /// Set things in motion so your service can do its work.  /// ﹤/summary﹥  protected override void OnStart(string[] args)  {  EventLog.WriteEntry("My simple service started.");  }   /// ﹤summary﹥  /// Stop this service.  /// ﹤/summary﹥  protected override void OnStop()  {  // TODO: Add code here to perform any tear-down necessary to stop your service.  }  }  }

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服务程序中如何为Windows服务创建安装项目”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!