c#開發的程式安裝時動態指定windows服務名稱

來源:互聯網
上載者:User

這下可把我難住了,難道要 在開發的代碼中一個一個地設定想要的名稱,然後重新編譯,再註冊成服務?
但是如果將來又要換個名稱呢?再重新設定、 編譯、註冊一遍?這樣操作太麻煩了!
於是我就想能不能通過在安裝的時候進行配置,比如加一個xml檔案記錄要安裝的服務的服務名等資訊,每次安裝前修改該xml檔案就可以了。
操作:
1、首先添加一個設定檔到服務主程式的根目錄,取名“ServiceSetting.xml”: 複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<ServiceName>testme</ServiceName>
<DisplayName>testmedisplay</DisplayName>
<Description>這裡僅僅是個測試而已</Description>
</Settings>

2、然後添加一個類檔案到服務主程式的根目錄,取名"SettingHelper.cs": 複製代碼 代碼如下: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()如下: 複製代碼 代碼如下: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 命令提示”,右鍵“以管理員身份運行”。
在命令列中輸入以下命令: 複製代碼 代碼如下:Setting environment for using Microsoft Visual Studio 2008 x86 tools.
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”刪除服務。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.