c#開發(fā)的程序安裝時動態(tài)指定windows服務(wù)名稱
更新時間:2012年06月09日 00:19:19 作者:
前段時間由于項目的需求,要在Windows里把同樣的組件制作成多個不同名稱的服務(wù),這些服務(wù)完成類似的功能,僅需要修改業(yè)務(wù)配置文件
這下可把我難住了,難道要 在開發(fā)的代碼中一個一個地設(shè)置想要的名稱,然后重新編譯,再注冊成服務(wù)?
但是如果將來又要換個名稱呢?再重新設(shè)置、 編譯、注冊一遍?這樣操作太麻煩了!
于是我就想能不能通過在安裝的時候進(jìn)行配置,比如加一個xml文件記錄要安裝的服務(wù)的服務(wù)名等信息,每次安裝前修改該xml文件就可以了。
操作:
1、首先添加一個配置文件到服務(wù)主程序的根目錄,取名“ServiceSetting.xml”:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<ServiceName>testme</ServiceName>
<DisplayName>testmedisplay</DisplayName>
<Description>這里僅僅是個測試而已</Description>
</Settings>
2、然后添加一個類文件到服務(wù)主程序的根目錄,取名"SettingHelper.cs":
SettingHelper
#region 文件描述
//-------------------------------------------------------------------------------------------------
// 描述:服務(wù)安裝配置幫助類
// 作者:鮑昊晟
// 時間:2012-05-10
//-------------------------------------------------------------------------------------------------
#endregion
using System;
using System.IO;
using System.Xml;
/// <summary>
/// 服務(wù)安裝配置幫助類
/// </summary>
internal class SettingHelper : IDisposable
{
#region 私有成員
private string _ServiceName;
private string _DisplayName;
private string _Description;
#endregion
#region 構(gòu)造函數(shù)
/// <summary>
/// 初始化服務(wù)配置幫助類
/// </summary>
public SettingHelper()
{
InitSettings();
}
#endregion
#region 屬性
/// <summary>
/// 系統(tǒng)用于標(biāo)志此服務(wù)的名稱
/// </summary>
public string ServiceName
{
get { return _ServiceName; }
}
/// <summary>
/// 向用戶標(biāo)志服務(wù)的友好名稱
/// </summary>
public string DisplayName
{
get { return _DisplayName; }
}
/// <summary>
/// 服務(wù)的說明
/// </summary>
public string Description
{
get { return _Description; }
}
#endregion
#region 私有方法
#region 初始化服務(wù)配置信息
/// <summary>
/// 初始化服務(wù)配置信息
/// </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("未能找到服務(wù)名稱配置文件 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文件,在修改構(gòu)造函數(shù)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、執(zhí)行安裝命令:
在開始菜單中找到“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、當(dāng)出現(xiàn)以下文字的時候就表明安裝成功了
安裝成功提示信息
Microsoft (R) .NET Framework 安裝實用工具版本 2.0.50727.5420
版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
正在運行事務(wù)處理安裝。
正在開始安裝的“安裝”階段。
查看日志文件的內(nèi)容以獲得 d:\wsinstalltest.exe 程序集的進(jìn)度。
該文件位于 。
正在安裝程序集“d:\wsinstalltest.exe”。
受影響的參數(shù)是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
正在安裝服務(wù) testme...
已成功安裝服務(wù) testme。
正在日志 Application 中創(chuàng)建 EventLog 源 testme...
“安裝”階段已成功完成,正在開始“提交”階段。
查看日志文件的內(nèi)容以獲得 d:\wsinstalltest.exe 程序集的進(jìn)度。
該文件位于 。
正在提交程序集“d:\wsinstalltest.exe”。
受影響的參數(shù)是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
“提交”階段已成功完成。
已完成事務(wù)處理安裝。
C:\Windows\system32>
可以進(jìn)入“服務(wù)”程序中查看剛才安裝的服務(wù)已經(jīng)安裝好了。
6、備注:
運行“sc start testme”啟動服務(wù);
運行“sc stop testme”停止服務(wù);
運行“sc delete testme”刪除服務(wù)。
但是如果將來又要換個名稱呢?再重新設(shè)置、 編譯、注冊一遍?這樣操作太麻煩了!
于是我就想能不能通過在安裝的時候進(jìn)行配置,比如加一個xml文件記錄要安裝的服務(wù)的服務(wù)名等信息,每次安裝前修改該xml文件就可以了。
操作:
1、首先添加一個配置文件到服務(wù)主程序的根目錄,取名“ServiceSetting.xml”:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<ServiceName>testme</ServiceName>
<DisplayName>testmedisplay</DisplayName>
<Description>這里僅僅是個測試而已</Description>
</Settings>
2、然后添加一個類文件到服務(wù)主程序的根目錄,取名"SettingHelper.cs":
復(fù)制代碼 代碼如下:
SettingHelper
#region 文件描述
//-------------------------------------------------------------------------------------------------
// 描述:服務(wù)安裝配置幫助類
// 作者:鮑昊晟
// 時間:2012-05-10
//-------------------------------------------------------------------------------------------------
#endregion
using System;
using System.IO;
using System.Xml;
/// <summary>
/// 服務(wù)安裝配置幫助類
/// </summary>
internal class SettingHelper : IDisposable
{
#region 私有成員
private string _ServiceName;
private string _DisplayName;
private string _Description;
#endregion
#region 構(gòu)造函數(shù)
/// <summary>
/// 初始化服務(wù)配置幫助類
/// </summary>
public SettingHelper()
{
InitSettings();
}
#endregion
#region 屬性
/// <summary>
/// 系統(tǒng)用于標(biāo)志此服務(wù)的名稱
/// </summary>
public string ServiceName
{
get { return _ServiceName; }
}
/// <summary>
/// 向用戶標(biāo)志服務(wù)的友好名稱
/// </summary>
public string DisplayName
{
get { return _DisplayName; }
}
/// <summary>
/// 服務(wù)的說明
/// </summary>
public string Description
{
get { return _Description; }
}
#endregion
#region 私有方法
#region 初始化服務(wù)配置信息
/// <summary>
/// 初始化服務(wù)配置信息
/// </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("未能找到服務(wù)名稱配置文件 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文件,在修改構(gòu)造函數(shù)public ProjectInstaller()如下:
復(fù)制代碼 代碼如下:
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、執(zhí)行安裝命令:
在開始菜單中找到“Microsoft Visual Studio 2008”-->“Visual Studio Tools”-->“Visual Studio 2008 命令提示”,右鍵“以管理員身份運行”。
在命令行中輸入以下命令:
復(fù)制代碼 代碼如下:
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
C:\Windows\system32>installutil /logfile d:\wsinstalltest.exe
5、當(dāng)出現(xiàn)以下文字的時候就表明安裝成功了
復(fù)制代碼 代碼如下:
安裝成功提示信息
Microsoft (R) .NET Framework 安裝實用工具版本 2.0.50727.5420
版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
正在運行事務(wù)處理安裝。
正在開始安裝的“安裝”階段。
查看日志文件的內(nèi)容以獲得 d:\wsinstalltest.exe 程序集的進(jìn)度。
該文件位于 。
正在安裝程序集“d:\wsinstalltest.exe”。
受影響的參數(shù)是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
正在安裝服務(wù) testme...
已成功安裝服務(wù) testme。
正在日志 Application 中創(chuàng)建 EventLog 源 testme...
“安裝”階段已成功完成,正在開始“提交”階段。
查看日志文件的內(nèi)容以獲得 d:\wsinstalltest.exe 程序集的進(jìn)度。
該文件位于 。
正在提交程序集“d:\wsinstalltest.exe”。
受影響的參數(shù)是:
logtoconsole =
assemblypath = d:\wsinstalltest.exe
logfile =
“提交”階段已成功完成。
已完成事務(wù)處理安裝。
C:\Windows\system32>
可以進(jìn)入“服務(wù)”程序中查看剛才安裝的服務(wù)已經(jīng)安裝好了。
6、備注:
運行“sc start testme”啟動服務(wù);
運行“sc stop testme”停止服務(wù);
運行“sc delete testme”刪除服務(wù)。
您可能感興趣的文章:
- Windows系統(tǒng)中使用C#編寫藍(lán)牙通信程序的簡單實例
- c#.NET中日志信息寫入Windows日志中解決方案
- 基于C#實現(xiàn)的仿windows左側(cè)伸縮菜單效果
- JavaScript與C# Windows應(yīng)用程序交互方法
- c#創(chuàng)建windows服務(wù)入門教程實例
- C#編寫Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
- 基于C#實現(xiàn)Windows服務(wù)狀態(tài)啟動和停止服務(wù)的方法
- C#通過創(chuàng)建Windows服務(wù)啟動程序的方法詳解
- C#編寫Windows服務(wù)實例代碼
- C#用Topshelf創(chuàng)建Windows服務(wù)的步驟分享
相關(guān)文章
C#使用ScrapySharp快速從網(wǎng)頁采集數(shù)據(jù)
這篇文章介紹了使用ScrapySharp快速從網(wǎng)頁采集數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

