通过配置文件动态改变windows服务程序的名字 dynamic change windows service name using the configuration file

本文讨论了使用动态名称安装Windows服务的方法。通过配置文件设置服务名,并介绍了如何避免使用安装工具时出现的异常。提供了代码示例以实现从指定文件中读取前缀并将其作为服务名称的一部分。

dynamic windows service name


Hi all,

I am trying to install a service using dynamic naming. I wrote values in the
configuration file, and trying to assign one of those values to a string,
and using this string as the name of the installer



<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="StringName" value="StringValue" />

</appSettings>

</configuration>



//Code in the service installer

str=System.Configuration.ConfigurationSettings.App Settings["StringName"];

this.serviceInstaller1.ServiceName=str;



when using the installutil tool, I get the following exception:

An exception occurred during the Install phase.

System.ArgumentException: Must specify value for source.



Usually, if I write the string value in a normal text file, and read the
value from the file while installing (instead of reading from the
appSettings element) I don't get any exceptions and it works fine..

So what could be the reason then? and normally how can you set a dynamic
service name using the configuration file?

Thank you for your help

 

 

Re: dynamic windows service name


Hello

The config file should have the name filename.exe.config, and must be in the
same directory as the executable file. Since you are using .NET framework's
installutil.exe to install your service. In this case, you must name the
file installutil.exe.config and put it in the .NET framework folder under
the system directory. This is not practical of course.

Best regards,
Sherif

 

 

Re: dynamic windows service name


Sherif has told you why that won't work. What I do for that is:


private string ServicePrefix {
get {
Assembly currentAsm = Assembly.GetExecutingAssembly();
FileInfo fi = new FileInfo(currentAsm.Location);
DirectoryInfo runFolder = fi.Directory;
string prefix = "";
foreach (FileInfo extFi in
runFolder.GetFiles("ServiceName.*")) {
prefix =
extFi.Extension.Replace(".","").ToUpper();
}
return prefix;
}
}

public ProjectInstaller() {
// This call is required by the Designer.
InitializeComponent();
string serviceName = this.ServicePrefix
+ this.serviceInstaller1.ServiceName;
Console.Write("ServiceName is " + serviceName + " (enter to
continue) ");
Console.ReadLine();
this.serviceInstaller1.DisplayName = serviceName;
}

This looks for a file called ServiceName.* in the same folder as the
Service's exe and whatever the * translates to is used as the prefix
of the service name as set at design time.

This allows me to have multiple copies of my service running on the
same machine.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值