源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-02-14 22:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:c#开发的程序安装时动态指定windows服务名称
这下可把我难住了,难道要 在开发的代码中一个一个地设置想要的名称,然后重新编译,再注册成服务? 但是如果将来又要换个名称呢?再重新设置、 编译、注册一遍?这样操作太麻烦了! 于是我就想能不能通过在安装的时候进行配置,比如加一个xml文件记录要安装的服务的服务名等信息,每次安装前修改该xml文件就可以了。 操作: 1、首先添加一个配置文件到服务主程序的根目录,取名“ServiceSetting.xml”:
[u]复制代码[/u] 代码如下:
<?xml version="1.0" encoding="utf-8" ?> <Settings> <ServiceName>testme</ServiceName> <DisplayName>testmedisplay</DisplayName> <Description>这里仅仅是个测试而已</Description> </Settings>
2、然后添加一个类文件到服务主程序的根目录,取名"SettingHelper.cs":
[u]复制代码[/u] 代码如下:
SettingHelper #region 文件描述 //------------------------------------------------------------------------------------------------- // 描述:服务安装配置帮助类 // 作者:鲍昊晟 // 时间:2012-05-10 //------------------------------------------------------------------------------------------------- #endregion using System; using System.IO; using System.Xml; /// <summary> /// 服务安装配置帮助类 /// </summary> internal class SettingHelper : IDisposable { #region 私有成员 private string _ServiceName; private string _DisplayName; private string _Description; #endregion #region 构造函数 /// <summary> /// 初始化服务配置帮助类 /// </summary> public SettingHelper() { InitSettings(); } #endregion #region 属性 /// <summary> /// 系统用于标志此服务的名称 /// </summary> public string ServiceName { get { return _ServiceName; } } /// <summary> /// 向用户标志服务的友好名称 /// </summary> public string DisplayName { get { return _DisplayName; } } /// <summary> /// 服务的说明 /// </summary> public string Description { get { return _Description; } } #endregion #region 私有方法 #region 初始化服务配置信息 /// <summary> /// 初始化服务配置信息 /// </summary> private void InitSettings() { string root = System.Reflection.Assembly.GetExecutingAssembly().Location; string xmlfile = root.Remove(root.LastIndexOf('\\') + 1) + "ServiceSetting.xml"; if (File.Exists(xmlfile)) { XmlDocument doc = new XmlDocument(); doc.Load(xmlfile); XmlNode xn = doc.SelectSingleNode("Settings/ServiceName"); _ServiceName = xn.InnerText; xn = doc.SelectSingleNode("Settings/DisplayName"); _DisplayName = xn.InnerText; xn = doc.SelectSingleNode("Settings/Description"); _Description = xn.InnerText; doc = null; } else { throw new FileNotFoundException("未能找到服务名称配置文件 ServiceSetting.xml!"); } } #endregion #endregion #region IDisposable 成员 private bool disposed = false; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { //managed dispose _ServiceName = null; _DisplayName = null; _Description = null; } //unmanaged dispose } disposed = true; } ~SettingHelper() { Dispose(false); } #endregion }
3、修改ProjectInstaller.cs文件,在修改构造函数public ProjectInstaller()如下:
[u]复制代码[/u] 代码如下:
ProjectInstaller using System.ComponentModel; using System.Configuration.Install; namespace WSInstallTest { [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); using (SettingHelper setting = new SettingHelper()) { serviceInstaller1.ServiceName = setting.ServiceName; serviceInstaller1.DisplayName = setting.DisplayName; serviceInstaller1.Description = setting.Description; } } //end of class } }
4、执行安装命令: 在开始菜单中找到“Microsoft Visual Studio 2008”-->“Visual Studio Tools”-->“Visual Studio 2008 命令提示”,右键“以管理员身份运行”。 在命令行中输入以下命令:
[u]复制代码[/u] 代码如下:
Setting environment for using Microsoft Visual Studio 2008 x86 tools. C:\Windows\system32>installutil /logfile d:\wsinstalltest.exe
5、当出现以下文字的时候就表明安装成功了
[u]复制代码[/u] 代码如下:
安装成功提示信息 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”删除服务。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部