通过自定义MSI安装程序修改app.config [英] Modifying app.config via custom msi installer
问题描述
我需要在app.config中创建一个地址字符串,如下所示:
I need to create an address string in app.config as:
<client>
<endpoint address="http://ServerName/xxx/yyy.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IClientIInfoService"
contract="DocuHealthLinkSvcRef.IClientIInfoService" name="BasicHttpBinding_IClientIInfoService" />
</client>
在安装过程中,用户需要输入ServerName
.
为此,我在安装程序中创建了一个新的UI对话框.我还编写了一个Installer.cs
类,并将install ()
重写为:
The ServerName
need to be entered by the user during installation.
For that i have created a new UI dialog in the Installer. I have also written an Installer.cs
class and overrided the install ()
as:
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string ServerName = Context.Parameters["ServerName"];
System.Diagnostics.Debugger.Break();
string exePath = string.Format("{0}myapp.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["ServerName"].Value = ServerName;
config.Save();
}
}
但是我该如何在我的app.config
中使用此ServerName
来创建指定的字符串.
我正在开发VS2010.
But how do i use this ServerName
in my app.config
to create the specified string.
I'm working on VS2010.
推荐答案
假定您正在使用app.config中完整的ServiceModel部分组
Assuming you are using the full ServiceModel section group in the app.config
基本上,请按照以下步骤操作:
Essentially you follow these steps:
- 加载ServiceModel配置部分
- 获取客户部分
- 获取ChannelEndpoint元素
- 通过用输入的值替换字符串"ServerName"来更改地址值
- 将地址"属性设置为新值
-
保存配置
- Load ServiceModel config section
- Get Client Section
- Get ChannelEndpoint Element
- Change Address value by replacing string "ServerName" with entered value
- Set Address attribute to new value
Save config
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string ServerName = Context.Parameters["ServerName"];
System.Diagnostics.Debugger.Break();
string exePath = string.Format("{0}myapp.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["ServerName"].Value = ServerName;
//Get ServiceModelSectionGroup from config
ServiceModelSectionGroup group = ServiceModelSectionGroup.GetSectionGroup (config);
//get the client section
ClientSection clientSection = group.Client;
//get the first endpoint
ChannelEndpointElement channelEndpointElement = clientSection.Endpoints[0];
//get the address attribute and replace servername in the string.
string address = channelEndpointElement.Address.ToString().Replace("ServerName", ServerName);
//set the Address attribute to the new value
channelEndpointElement.Address = new Uri(address);
config.Save();
}
这篇关于通过自定义MSI安装程序修改app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!