通过自定义MSI安装程序修改app.config [英] Modifying app.config via custom msi installer

查看:126
本文介绍了通过自定义MSI安装程序修改app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在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:

  1. 加载ServiceModel配置部分
  2. 获取客户部分
  3. 获取ChannelEndpoint元素
  4. 通过用输入的值替换字符串"ServerName"来更改地址值
  5. 将地址"属性设置为新值
  6. 保存配置

  1. Load ServiceModel config section
  2. Get Client Section
  3. Get ChannelEndpoint Element
  4. Change Address value by replacing string "ServerName" with entered value
  5. Set Address attribute to new value
  6. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆