从XML文件安装InstallShield动态配置

时间:2012-06-14 23:57:48

标签: installshield

InstallShield包括通过XML文件更改修改应用程序.config文件的功能* ...但是,如果要跨多个安装包共享配置设置,该怎么办?

在我们的环境中,我们提供WPF应用程序和WinForms应用程序,其中一个是管理控制台,另一个是数据收集应用程序(用户选择他们想要的单个安装程序)。我们还为两个应用程序使用的服务层提供安装程序,以便每个客户位置的Web服务端点都不同。 (实际环境有点复杂,但以上是复杂性的一个例子)

我们的方向是创建一个XML文件,该文件包含两个安装程序的通用配置设置,包括常用设置,InstallShield读取这些值,然后更新每个应用程序的.config文件以指向相同的端点。有人做过吗?这可以在不使用InstallScript或自定义操作的情况下完成吗?

  • 将xml配置文件导入XML File Changes区域以读取配置文件中的每一行,并通过XPath为每一行创建添加请求(例如add [@ key =“IsMultiTouch”和@ value =“True” ])。这似乎不太理想,因为我怀疑它只是重新创建我希望它从开发团队在文件的当前状态中读取的文件,并且仅在安装期间修改appsettings中的值

2 个答案:

答案 0 :(得分:1)

[随访] 使用CtrlGetText()从InstallShield对话框中拉出值,将该数据传递给我的dll以包含在我的应用程序配置文件中。生成的.config文件条目包含大量“ ”序列。 尝试用InstallScript清理它们并不是很好。 最终必须通过将XmlDocument翻转成字符串,在字符串上执行.Replace(“ ”,“”),将其加载到XmlDocument(以确保它有效),然后保存它来清理它们在dll中。 InstallShield!=太棒了。 [/随访]

XML文件更改似乎不是一个好方向,所以我编写了一个自定义操作,在安装该功能后触发。自定义操作调用托管程序集,该程序集通过xml文件旋转,应用对应用程序.config文件的更改。

自定义操作

#define CO_APPCONFIG_DLL "MyCompany.InstallShield.AppConfig.dll"
#define CO_APPCONFIG_FQNAME "MyCompany.InstallShield.AppConfig.ConfigMgr"

#define CO_APPSETTINGS_NEW "MyApp_AppSettings.xml"
#define CO_APPSETTINGS_CONFIG "MyCompany.IOAnywhere.Dashboard.exe.config"
#define CO_APPSETTINGS_SECTION "CO_dashboard"

//---------------------------------------------------------------------------
// The Installed event is sent after the feature Dashboard
// is installed.
//---------------------------------------------------------------------------
export prototype Dashboard_Installed();
function Dashboard_Installed()
BOOL bResult;
OBJECT oAppConfig;
begin

try
    // Note: the configuration dll is in the support directory
    set oAppConfig = DotNetCoCreateObject(SUPPORTDIR ^ CO_APPCONFIG_DLL, CO_APPCONFIG_FQNAME, "");
catch
    MessageBox("Error Loading" + SUPPORTDIR ^ CO_APPCONFIG_DLL + ": " + Err.Number + " " + Err.Description, INFORMATION); 
    abort;
endcatch;

try
    // Note: the new configuration settings file should be in the same folder as the setup.exe
    bResult = oAppConfig.ConfigureSettings(CO_APPSETTINGS_NEW, TARGETDIR ^ CO_APPSETTINGS_CONFIG, CO_APPSETTINGS_SECTION);
catch
    MessageBox("Verify that the file " + CO_APPSETTINGS_NEW + " exists in the setup directory.  Error calling ConfigureSettings " + SUPPORTDIR ^ CO_APPCONFIG_DLL + " " + Err.Number + " " + Err.Description, INFORMATION);
endcatch;

end;

调用DLL

using System;
using System.Xml;

/// <summary>
/// Called by InstallShield Installer process to apply appSettings to an installed applications .config file
/// </summary>
namespace CO.InstallShield.AppConfig
{
    /// <summary>
    /// ConfigMgr is the class that encapsulates functionality related to modifying a .config file
    /// </summary>
    public class ConfigMgr
    {
        /// <summary>
        /// ConfigureSettings applies changes from a common xml file to the applications .config file
        /// </summary>
        /// <remarks>
        /// Ensures required keys for the application are included in the .config file
        /// Applies common settings to the .config file
        /// Applies application specific settings to the .config file
        /// </remarks>
        /// <param name="configFilePath">Path to the xml file that has the setting that need to be applied</param>
        /// <param name="targetAppConfigPath">Path to the .config file for the appliction</param>
        /// <param name="targetAppName">Section in the xml file that has application specific settings</param>
        /// <returns>True if it was able to configure settings</returns>
        public bool ConfigureSettings(string configFilePath, string targetAppConfigPath, string targetAppName)
        {
            bool completed = false;

            try
            {
                XmlDocument configFileDoc = new XmlDocument();
                configFileDoc.Load(configFilePath);

                XmlDocument targetAppConfigDoc = new XmlDocument();
                targetAppConfigDoc.Load(targetAppConfigPath);

                // ensure the appSettings section exists
                AddRequiredSections(ref targetAppConfigDoc);

                // ensure all required keys exist in the target .config file
                AddRequiredKeys(configFileDoc.SelectSingleNode("configuration/" + targetAppName + "/requiredKeys"), ref targetAppConfigDoc);

                // loop through each key in the common section of the configuration file
                AddKeyValues(configFileDoc.SelectSingleNode("configuration/common/appSettings"), ref targetAppConfigDoc);

                // loop through each key in the app specific section of the configuration file - it will override the standard configuration
                AddKeyValues(configFileDoc.SelectSingleNode("configuration/" + targetAppName + "/appSettings"), ref targetAppConfigDoc);

                // save it off
                targetAppConfigDoc.Save(targetAppConfigPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                completed = true;
            }
            return completed;
        }
        private void AddRequiredSections(ref XmlDocument targeAppConfigDoc)
        {
            // Ensure the target .config file has an appSettings section... unusual but evidently possible
            if (targeAppConfigDoc.SelectSingleNode("configuration/appSettings") == null)
                targeAppConfigDoc.SelectSingleNode("configuration").AppendChild(targeAppConfigDoc.CreateNode(XmlNodeType.Element, "appSettings", null));
        }
        private void AddKeyValues(XmlNode configAppNodeSet, ref XmlDocument targetAppConfigDoc)
        {
            if (configAppNodeSet == null) return; // Nothing to do

            foreach (XmlNode configNode in configAppNodeSet.SelectNodes("add"))
            {
                XmlNode targetNode = targetAppConfigDoc.SelectSingleNode("configuration/appSettings/add[@key='" + configNode.Attributes["key"].Value + "']");
                if (targetNode != null)
                {
                    targetNode.Attributes["value"].Value = configNode.Attributes["value"].Value;
                }
            }
        }
        private void AddRequiredKeys(XmlNode targetAppNodeSet, ref XmlDocument targetAppConfigDoc)
        {
            if (targetAppNodeSet == null) return; // Nothing to do

            foreach (XmlNode targetNode in targetAppNodeSet.SelectNodes("key"))
            {
                // add the key if it doesn't already exist
                XmlNode appNode = targetAppConfigDoc.SelectSingleNode("configuration/appSettings/add[@key='" + targetNode.Attributes["value"].Value + "']");
                if (appNode == null)
                {
                    appNode = targetAppConfigDoc.SelectSingleNode("configuration/appSettings");
                    XmlNode newAddNode = targetAppConfigDoc.CreateNode(XmlNodeType.Element, "add", null);
                    XmlAttribute newAddNodeKey = targetAppConfigDoc.CreateAttribute("key");
                    newAddNodeKey.Value = targetNode.Attributes["value"].Value;
                    XmlAttribute newAddNodeValue = targetAppConfigDoc.CreateAttribute("value");
                    newAddNodeValue.Value = "NotSet";
                    newAddNode.Attributes.Append(newAddNodeKey);
                    newAddNode.Attributes.Append(newAddNodeValue);
                    appNode.AppendChild(newAddNode);
                }
            }
        }
    }
}

答案 1 :(得分:0)

从安装的角度来看,你可以通过遵循remember-property模式的变体来获得很多。通过存储XML文件更改引用的属性值,创建一个共享组件,在共享注册表项中注册各种配置信息。使用系统搜索尝试将这些值读回到相同或相关安装的后续运行中。但是,此方法不会更新相关项目中现有的单独配置文件。

为了解决这个问题(虽然您可能会发现单独的配置本身就很有用),但我建议您找一种方法来引用单个共享配置。这可能是通过使用配置文件的共享目录。可能是通过直接引用共享注册表位置并跳过之前描述的XML文件更改。