c#开发的程序安装时动态指定windows服务名称

转自:http://www.jb51.net/article/30549.htm

前段时间由于项目的需求,要在Windows里把同样的组件制作成多个不同名称的服务,这些服务完成类似的功能,仅需要修改业务配置文件

这下可把我难住了,难道要 在开发的代码中一个一个地设置想要的名称,然后重新编译,再注册成服务? 
但是如果将来又要换个名称呢?再重新设置、 编译、注册一遍?这样操作太麻烦了! 
于是我就想能不能通过在安装的时候进行配置,比如加一个xml文件记录要安装的服务的服务名等信息,每次安装前修改该xml文件就可以了。 
操作: 
1、首先添加一个配置文件到服务主程序的根目录,取名“ServiceSetting.xml”:

1 <?xml version="1.0" encoding="utf-8" ?>
2 <Settings>
3 <ServiceName>testme</ServiceName>
4 <DisplayName>testmedisplay</DisplayName>
5 <Description>这里仅仅是个测试而已</Description>
6 </Settings> 

2、然后添加一个类文件到服务主程序的根目录,取名"SettingHelper.cs":

  1 SettingHelper
  2 #region 文件描述
  3 //-------------------------------------------------------------------------------------------------
  4 // 描述:服务安装配置帮助类
  5 // 作者:鲍昊晟
  6 // 时间:2012-05-10
  7 //-------------------------------------------------------------------------------------------------
  8 #endregion
  9 using System;
 10 using System.IO;
 11 using System.Xml;
 12 /// <summary>
 13 /// 服务安装配置帮助类
 14 /// </summary>
 15 internal class SettingHelper : IDisposable
 16 {
 17 #region 私有成员
 18 private string _ServiceName;
 19 private string _DisplayName;
 20 private string _Description;
 21 #endregion
 22 #region 构造函数
 23 /// <summary>
 24 /// 初始化服务配置帮助类
 25 /// </summary>
 26 public SettingHelper()
 27 {
 28 InitSettings();
 29 }
 30 #endregion
 31 #region 属性
 32 /// <summary>
 33 /// 系统用于标志此服务的名称
 34 /// </summary>
 35 public string ServiceName
 36 {
 37 get { return _ServiceName; }
 38 }
 39 /// <summary>
 40 /// 向用户标志服务的友好名称
 41 /// </summary>
 42 public string DisplayName
 43 {
 44 get { return _DisplayName; }
 45 }
 46 /// <summary>
 47 /// 服务的说明
 48 /// </summary>
 49 public string Description
 50 {
 51 get { return _Description; }
 52 }
 53 #endregion
 54 #region 私有方法
 55 #region 初始化服务配置信息
 56 /// <summary>
 57 /// 初始化服务配置信息
 58 /// </summary>
 59 private void InitSettings()
 60 {
 61 string root = System.Reflection.Assembly.GetExecutingAssembly().Location;
 62 string xmlfile = root.Remove(root.LastIndexOf(‘\\‘) + 1) + "ServiceSetting.xml";
 63 if (File.Exists(xmlfile))
 64 {
 65 XmlDocument doc = new XmlDocument();
 66 doc.Load(xmlfile);
 67 XmlNode xn = doc.SelectSingleNode("Settings/ServiceName");
 68 _ServiceName = xn.InnerText;
 69 xn = doc.SelectSingleNode("Settings/DisplayName");
 70 _DisplayName = xn.InnerText;
 71 xn = doc.SelectSingleNode("Settings/Description");
 72 _Description = xn.InnerText;
 73 doc = null;
 74 }
 75 else
 76 {
 77 throw new FileNotFoundException("未能找到服务名称配置文件 ServiceSetting.xml!");
 78 }
 79 }
 80 #endregion
 81 #endregion
 82 #region IDisposable 成员
 83 private bool disposed = false;
 84 public void Dispose()
 85 {
 86 Dispose(true);
 87 GC.SuppressFinalize(this);
 88 }
 89 protected virtual void Dispose(bool disposing)
 90 {
 91 if (!this.disposed)
 92 {
 93 if (disposing)
 94 {
 95 //managed dispose
 96 _ServiceName = null;
 97 _DisplayName = null;
 98 _Description = null;
 99 }
100 //unmanaged dispose
101 }
102 disposed = true;
103 }
104 ~SettingHelper()
105 {
106 Dispose(false);
107 }
108 #endregion
109 } 

3、修改ProjectInstaller.cs文件,在修改构造函数public ProjectInstaller()如下:

 1 ProjectInstaller
 2 using System.ComponentModel;
 3 using System.Configuration.Install;
 4 namespace WSInstallTest
 5 {
 6 [RunInstaller(true)]
 7 public partial class ProjectInstaller : Installer
 8 {
 9 public ProjectInstaller()
10 {
11 InitializeComponent();
12 using (SettingHelper setting = new SettingHelper())
13 {
14 serviceInstaller1.ServiceName = setting.ServiceName;
15 serviceInstaller1.DisplayName = setting.DisplayName;
16 serviceInstaller1.Description = setting.Description;
17 }
18 }
19 //end of class
20 }
21 } 

4、执行安装命令: 
在开始菜单中找到“Microsoft Visual Studio 2008”-->“Visual Studio Tools”-->“Visual Studio 2008 命令提示”,右键“以管理员身份运行”。 
在命令行中输入以下命令:

1 Setting environment for using Microsoft Visual Studio 2008 x86 tools.
2 C:\Windows\system32>installutil /logfile d:\wsinstalltest.exe 

5、当出现以下文字的时候就表明安装成功了

安装成功提示信息
Microsoft (R) .NET Framework 安装实用工具版本 2.0.50727.5420
版权所有(C) Microsoft Corporation。保留所有权利。
正在运行事务处理安装。
正在开始安装的“安装”阶段。
查看日志文件的内容以获得 d:\wsinstalltest.exe 程序集的进度。
该文件位于 。
正在安装程序集“d:\wsinstalltest.exe”。
受影响的参数是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
正在安装服务 testme...
已成功安装服务 testme。
正在日志 Application 中创建 EventLog 源 testme...
“安装”阶段已成功完成,正在开始“提交”阶段。
查看日志文件的内容以获得 d:\wsinstalltest.exe 程序集的进度。
该文件位于 。
正在提交程序集“d:\wsinstalltest.exe”。
受影响的参数是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
“提交”阶段已成功完成。
已完成事务处理安装。
C:\Windows\system32> 

可以进入“服务”程序中查看刚才安装的服务已经安装好了。 
6、备注: 
运行“sc start testme”启动服务; 
运行“sc stop testme”停止服务; 
运行“sc delete testme”删除服务。

时间: 2024-08-08 00:46:00

c#开发的程序安装时动态指定windows服务名称的相关文章

VC项目程序运行时设置指定目录读取Dll

方法一: 选择当前工程,右击"Properties" -> "Configuration Properties" -> "Debugging",在"Working Directory"设置dll的路径就可以了 方法二:设置项目的环境变量 方法三: CString strDllPath = GetExePath() + _T("System"); SetDllDirectory(strDllPat

linux c开发: 在程序退出时进行处理

有时候,希望程序退出时能进行一些处理,比如保存状态,释放一些资源.c语言开发的linux程序,有可能正常退出(exit),有可能异常crash,而异常crash可能是响应了某信号的默认处理.这里总结一下这些情况,如何获取一个统一的退出处理的点,说白了就是写一个回调函数,让他在程序正常或异常退出时调用. 先看正常退出,即调用exit或者main函数return亦或最后一个线程正常退出时,如何捕获退出事件. 使用atexit函数.头文件:#include<stdlib.h>, 函数原型:void

.Net 指定时间段内定时执行的Windows服务(System.Threading.Thread)

创建一个Windows服务项目:解决方案(右击)——> 添加 ——> 新建项目——>项目类型选择Windows——>模板选择Windows服务 ,如图: 编写Windows服务程序创建后会生成两个文件 Program.cs 和 Service1.cs(我已重命名为MyService.cs),编写服务内容:具体服务代码: 1 using System; 2 using System.Configuration; 3 using System.ServiceProcess; 4 usi

【源码下载】分享一个支持自安装自卸载的Windows服务

这个程序来自  www.codeproject.com 具体的出处就忘了 服务器端的程序,我一般采用在windows服务中调用打开的方式,这样既能看到界面,又能避免系统注销时,程序跟着退出.而且能够支持开机启动. 只在windows server 2003 和.net2.0环境下使用,其他运行环境没有测试 点击服务程序能够实现自安装和自卸载,用起来还是很方便的. 在配置文件中,设定了服务器端程序的位置地址 服务器端程序是另一个程序,用来和客户端进行通讯.windows服务用来打开这个服务器端程序

通过代码动态创建Windows服务

开发完Windows服务后,一般通过如下命令进行注册Windows服务 @echo off %SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\installutil.exe %~dp0\服务程序.exe pause 除了脚本的方式,通过代码,也可以注册Windows服务: var ti = new TransactedInstaller(); ti.Installers.Add(new ServiceProcessInstaller { Acco

Mongodb安装为32位windows服务

cmd启动mongodb这里就不说了,比较简单. 要想在32位的机器上把mongodb安装为windows服务,就必须开启journal.journal类似于关系数据库的redo.log,由于mongodb认为开启journal比较消耗内存,所以在32位的机器上默认不开启的(64位是默认开启的).下面就以配置文件启动的方式安装mongodb的windows服务. 新建mongod.cfg文件,放在mongodb安装文件的bin目录下,内容如下:journal=truelogpath=D:\mon

MongoDB安装并设置为windows服务以使其开机自启

在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-3.2.1.zip/download 1.解压zip文件,我的路径为:F:\StudyTools\MongoDB 2.设置数据文件路径: 在F:\StudyTools\MongoDB里面新建log和data文件夹 在F:\StudyTools\MongoDB\log里面新建mongod

MongoDB安装及添加到Windows服务,随系统启动

本文介绍在Windows环境下安装MongoDB及添加到Windows服务中,随系统启动 首先去官网下载Windows安装包:https://www.mongodb.org/downloads 一般情况下选择默认的64位即可(除非你的主机是32位),下载后,假设我们安装到:D:/soft/mongodb/ ,完成后,进入该目录,新建data和logs两个文件夹,并在logs目录下再新建个mongodb.log.然后打开命令提示符(CMD),Windows8.1的用户需要用管理员身份打开.执行以下

C#winform程序安装时自动卸载新版本覆盖旧版本

vs2005为winform程序做的安装包.在以有程序旧版本的机子上用新版本的安装包安装软件时提示  “以经安装该产品的另一个版本.无法继续安装此版本........” 在安装部署项目中设“RemovePreviousVersion”为true后也不行.  还是提示要卸载旧版本才能安装新版本..如何设置在安装新版本时复盖掉旧版本?? 1.确认两个版本的RemovePreviousVersion设置都是true的 2.确认两个版本有不同的ProductCode和相同的UpgradeCode 3.确