从自定义安装程序类动作config文件;更新<&APPNAME GT [英] Updating <appname>.config file from an Custom Installer Class Action

查看:122
本文介绍了从自定义安装程序类动作config文件;更新<&APPNAME GT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(通过.NET安装类动作 )试图安装更新时我的应用程序的config文件。但是,我似乎无法得到ConfigurationManager中列出任何属性或能够设置任何东西。

I've tried updating my application's .config file during installer (via a .NET Installer Class Action). But, I can't seem to get ConfigurationManager to list any properties or be able to set anything.

我学会了这种方法从该向我指出这几个职位计算器指南: http://raquila.com/software/配置 - 应用程序 - 配置 - 应用程序设置 - 在-MSI安装/

I learned of this approach from several stackoverflow posts that pointed me to this guide: http://raquila.com/software/configure-app-config-application-settings-during-msi-install/

我调查我的项目和他之间的差异,并发现我的配置文件被不同地格式化。我相信这是与事实,我使用设置文件做的。

I've investigated the differences between my project and his and noticed that my configuration files are formatted differently. I believe this has to do with the fact that i'm using "Settings" files.

配置文件:

<?xml version="1.0" encoding="utf-8" ?>     
<configuration>     
  <appSettings>      
    <add key="Param1" value="" />      
    <add key="Param2" value="" />      
    <add key="Param3" value="" />      
  </appSettings>      
</configuration>



在哪里我的样子:

Where mine looks like:

   <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="MyAppName.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MyAppName.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <MyAppName.Properties.Settings>
            <setting name="TESTSETTING" serializeAs="String">
                <value>asdfasdfasdf</value>
            </setting>
        </MyAppName.Properties.Settings>
        <MyAppName.Settings1>
            <setting name="VerboseErrorMode" serializeAs="String">
                <value>False</value>
            </setting>
    <applicationSettings>
        <MyAppName.Settings1>
            <setting name="RunOnStartup" serializeAs="String">
                <value>True</value>
            </setting>
        </MyAppName.Settings1>
    </applicationSettings>
</configuration>

要阐明了事情的原委一些轻......我试着打印出像设置列表所以:

To shed some light on what was going on... I tried printing out the list of settings like so:

            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);               

            // Try getting the Settings1 Section
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("Settings1");  // Also tried myNamespace.Settings1
            if (appSettings != null)
            {
                valList = "Settings1: ";
                foreach (string key in appSettings.Settings.AllKeys)
                {
                    string value = appSettings.Settings[key].Value;
                    valList += ("Key: '" + key + "' = '" + value + "'\n");
                }
            }
            else
            {
                valList = "appSettings was null";
            }
            MessageBox.Show(valList);

        MessageBox.Show(valList);



我已经试过这几个排列...并在所有情况下的输出的appSettings为空

I have tried several permutations of this... and in all cases output is "appSettings was null".

我也试过几种不同的方式初始化配置管理器...

I also tried initializing the configuration manager in several different ways...

            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            MessageBox.Show("Section Count: " + config.Sections.Count);
            MessageBox.Show("Has File: " + config.HasFile);
            MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared);

            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            MessageBox.Show("Section Count: " + config.Sections.Count);
            MessageBox.Show("Has File: " + config.HasFile);
            MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared);

有关他们每个人的部分计数恢复为20(我不知道那里的20来自......我本来期望它是3)。结果
HasFile是第一种情况下真假第二。声明

命名空间是在两种情况下错误的。

For each of them the section count returned was 20. (I have no idea where the 20 comes from... I would have expected it to be 3).
HasFile was true for the first case and false for the second.
Namespace Declared was false in both cases.

感谢

修改(09年6月18日):还在寻找到了这个问题。任何人有什么想法?谢谢

EDIT (6-18-09): Still looking into this question. Anyone else have any ideas? Thanks.

搜索关键词:对象引用未设置为未设置为实例< - 这试图写一个属性时出现。

Search Keywords: "Object Reference Not Set to not set to an instance" <-- this occurs when trying to write to a property.

推荐答案

我碰到了同样的问题来了,深入调查研究后,我发现最简单的方法来更新配置文件任何部分(如app.config)中,这是通过使用XPath。
我们有它连接到web服务的应用程序,在安装过程中,用户输入的web服务的URL,这应该被保存在以下app.config文件:

I came across the same problem, after deep investigation, I found out the easiest way to update any part of config files (e.g. app.config), that's by using XPath. We have an application which connects to web service, during the installation, the user enters the URL of the web service and this should be saved in the following app.config file:

    	<?xml version="1.0"?>
	<configuration>
	  <configSections>
		<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
		  <section name="ApplicationServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
		</sectionGroup>
	  </configSections>

	  <applicationSettings>
		<ApplicationServer.Properties.Settings>
		  <setting name="ApplicationServer_ApplicationServerUrl" serializeAs="String">
			<value>whatever comes from setup should go here</value>
		  </setting>
		</ApplicationServer.Properties.Settings>
	  </applicationSettings>
	</configuration>

下面是代码的安装程序类要做到这一点:

Here is the code to do this in the installer class:

public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);

        string targetDirectory = Context.Parameters["targetdir"];
        string param1 = Context.Parameters["param1"];

        string path = System.IO.Path.Combine(targetDirectory, "app.config");

		System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();

        xDoc.Load(path);

        System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Intellisense.ApplicationServer.Properties.Settings/setting[@name='ApplicationServer_ApplicationServerUrl']/value");
        node.InnerText = (param1.EndsWith("/") ? param1 : param1 + "/");

        xDoc.Save(path); // saves the web.config file  
    }



基本上,因为配置文件是一个基于XML的文档,我使用XPath表达式找到特定节点,并改变它的值。

Basically, since the config file is a XML based document, I am using XPath expression to locate specific node and change its value.

这篇关于从自定义安装程序类动作config文件;更新&LT;&APPNAME GT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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