欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#開(kāi)發(fā)的程序安裝時(shí)動(dòng)態(tài)指定windows服務(wù)名稱

 更新時(shí)間:2012年06月09日 00:19:19   作者:  
前段時(shí)間由于項(xiàng)目的需求,要在Windows里把同樣的組件制作成多個(gè)不同名稱的服務(wù),這些服務(wù)完成類似的功能,僅需要修改業(yè)務(wù)配置文件
這下可把我難住了,難道要 在開(kāi)發(fā)的代碼中一個(gè)一個(gè)地設(shè)置想要的名稱,然后重新編譯,再注冊(cè)成服務(wù)?
但是如果將來(lái)又要換個(gè)名稱呢?再重新設(shè)置、 編譯、注冊(cè)一遍?這樣操作太麻煩了!
于是我就想能不能通過(guò)在安裝的時(shí)候進(jìn)行配置,比如加一個(gè)xml文件記錄要安裝的服務(wù)的服務(wù)名等信息,每次安裝前修改該xml文件就可以了。
操作:
1、首先添加一個(gè)配置文件到服務(wù)主程序的根目錄,取名“ServiceSetting.xml”:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<ServiceName>testme</ServiceName>
<DisplayName>testmedisplay</DisplayName>
<Description>這里僅僅是個(gè)測(cè)試而已</Description>
</Settings>

2、然后添加一個(gè)類文件到服務(wù)主程序的根目錄,取名"SettingHelper.cs":
復(fù)制代碼 代碼如下:

SettingHelper
#region 文件描述
//-------------------------------------------------------------------------------------------------
// 描述:服務(wù)安裝配置幫助類
// 作者:鮑昊晟
// 時(shí)間: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ù)的說(shuō)明
/// </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í)行安裝命令:
在開(kāi)始菜單中找到“Microsoft Visual Studio 2008”-->“Visual Studio Tools”-->“Visual Studio 2008 命令提示”,右鍵“以管理員身份運(yùn)行”。
在命令行中輸入以下命令:
復(fù)制代碼 代碼如下:

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

5、當(dāng)出現(xiàn)以下文字的時(shí)候就表明安裝成功了
復(fù)制代碼 代碼如下:

安裝成功提示信息
Microsoft (R) .NET Framework 安裝實(shí)用工具版本 2.0.50727.5420
版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
正在運(yùn)行事務(wù)處理安裝。
正在開(kāi)始安裝的“安裝”階段。
查看日志文件的內(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...
“安裝”階段已成功完成,正在開(kāi)始“提交”階段。
查看日志文件的內(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、備注:
運(yùn)行“sc start testme”啟動(dòng)服務(wù);
運(yùn)行“sc stop testme”停止服務(wù);
運(yùn)行“sc delete testme”刪除服務(wù)。

相關(guān)文章

  • Unity通過(guò)腳本創(chuàng)建網(wǎng)格Mesh的方法

    Unity通過(guò)腳本創(chuàng)建網(wǎng)格Mesh的方法

    Unity中的網(wǎng)格作為組件不能脫離物體單獨(dú)存在,通過(guò)新建腳本來(lái)實(shí)現(xiàn)相關(guān)操作,本文重點(diǎn)給大家介紹Unity通過(guò)腳本創(chuàng)建網(wǎng)格Mesh的方法,感興趣的朋友一起看看吧
    2022-04-04
  • C#中循環(huán)語(yǔ)句:while、for、foreach的使用

    C#中循環(huán)語(yǔ)句:while、for、foreach的使用

    本篇文章主要介紹了C#中的三種循環(huán)語(yǔ)句(while、for、foreach)的實(shí)現(xiàn)方式,需要的朋友可以參考下
    2015-07-07
  • C#中使用UDP通信實(shí)例

    C#中使用UDP通信實(shí)例

    這篇文章主要介紹了C#中使用UDP通信實(shí)例,非常實(shí)用的技巧,需要的朋友可以參考下
    2014-08-08
  • Unity3D自定義創(chuàng)建圓錐體

    Unity3D自定義創(chuàng)建圓錐體

    這篇文章主要為大家詳細(xì)介紹了Unity3D自定義創(chuàng)建圓錐體,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#語(yǔ)言async?await之迭代器工作原理示例解析

    C#語(yǔ)言async?await之迭代器工作原理示例解析

    這篇文章主要為大家介紹了C#語(yǔ)言async?await之迭代器工作原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割

    Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割

    這篇文章主要為大家詳細(xì)介紹了Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • c# 如何實(shí)現(xiàn)獲取二維數(shù)組的列數(shù)

    c# 如何實(shí)現(xiàn)獲取二維數(shù)組的列數(shù)

    這篇文章主要介紹了c# 實(shí)現(xiàn)獲取二維數(shù)組的列數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • 淺析C# 索引器(Indexer)

    淺析C# 索引器(Indexer)

    這篇文章主要介紹了C# 索引器(Indexer)的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C# WebApi CORS跨域問(wèn)題解決方案

    C# WebApi CORS跨域問(wèn)題解決方案

    本篇文章主要介紹了C# WebApi CORS跨域問(wèn)題解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • C#中Datetimepicker出現(xiàn)問(wèn)題的解決方法

    C#中Datetimepicker出現(xiàn)問(wèn)題的解決方法

    這篇文章主要給大家介紹了關(guān)于C#中Datetimepicker出現(xiàn)問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評(píng)論