我们在一个项目中有同样的问题我正在努力,但我们采取了不同的方法。我们不是使用必须与可执行文件位于同一路径中的App.config文件,而是更改了安装程序类和服务的Main入口点。
我们这样做是因为我们不希望在不同位置使用相同的项目文件。这个想法是使用相同的分发文件,但使用不同的服务名称。
所以我们所做的就是我们的ProjectInstaller内:
private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
string keyPath = @"SYSTEM\CurrentControlSet\Services\" + this.serviceInstaller1.ServiceName;
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(keyPath, true);
// Pass the service name as a parameter to the service executable
if (ckey != null && ckey.GetValue("ImagePath")!= null)
ckey.SetValue("ImagePath", (string)ckey.GetValue("ImagePath") + " " + this.serviceInstaller1.ServiceName);
}
private void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
{
// Configura ServiceName e DisplayName
if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
{
this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
this.serviceInstaller1.DisplayName = this.Context.Parameters["ServiceName"];
}
}
private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
}
我们使用InstallUtil来安设我们这样的服务:
[FramerokPath]\installutil /ServiceName=[name] [ExeServicePath]
然后,你的应用程序的Main
切入点内,我们检查了args
属性以获取我们在AfterInstall事件中设置的服务的安装名称。
这种方法有一些问题,如:
- 我们不得不为没有参数安装服务创建一个默认名称。例如,如果没有名称传递给我们的服务,那么我们使用默认的名称;
- 您可以将传递给我们的应用程序的服务名称更改为与安装的名称不同。
本页面的解决方案对我来说并不完全适用。我可以得到正确的目录,但在调用SetCurrentDirectory之后,配置值仍然只是空字符串。你有没有做别的事情导致.config文件被加载后? – Sean 2013-07-15 18:58:50