我想使用 wix 动态设置功能目录路径#

I would like to set a features directory path dynamically using wix#

我正在尝试动态设置在 wix# 中创建的 wix 项目的路径。 None 个目录应由用户配置。其中两个目录应设置在任一 wix# 事件或自定义操作中。主目录会正常配置。

我还应该声明我是 wix 的新手,而且是 wix# 的新手。这是一些示例代码。您将需要创建辅助 ClassFoo.dll 项目并添加一个 Class1.cs 文件和一个名为 "test extra file.txt".

的文本文件
using System;
using System.Linq;
using IO = System.IO;
using System.Windows.Forms;
using WixSharp;
using WixSharp.Forms;
using Microsoft.Win32;
using Microsoft.Deployment.WindowsInstaller;
using System.Diagnostics;

namespace WixSharpSetup
{
    class Program
    {

#if DEBUG
        const string BUILD = "Debug";
#else
        const string BUILD = "Release";
#endif

        static void Main()
        {

            var binariesFeature = new Feature("Feature 1", "Feature 1", "FEATURE1_DIR");
            var extensionFeature = new Feature("Feature 2", "Feature 2", "FEATURE2_DIR");

            var project = new ManagedProject("My Company Test Product",
                //new ManagedAction(CustomActions.SetInstallPaths, Return.ignore, When.Before, Step.InstallInitialize, Condition.NOT_Installed),
                new Dir(new Id("FEATURE1_DIR"), binariesFeature, @"%ProgramFiles%\My Company\Test Product",
                    new File(new Id("FEATURE1_FILE1"), binariesFeature, @"..\ClassFoo\bin\" + BUILD + @"\ClassFoo.dll"))
/* **** THIS DIRECTORY NEEDS TO BE SET PROGRAMATICALLY TO THE FEATURE1_INSTALL_PATH *****/
                , new Dir(new Id("FEATURE1_FILE_DIR"), binariesFeature, "NOT_SET",
                    new File(new Id("FEATURE1_FILE2"), binariesFeature, @"..\ClassFoo\bin\" + BUILD + @"\test extra file.txt"))
/* **** THIS DIRECTORY NEEDS TO BE SET PROGRAMATICALLY TO THE FEATURE2_INSTALL_PATH *****/
                , new Dir(new Id("FEATURE2_DIR"), extensionFeature, @"NOT_SET",
                    new File(new Id("FEATURE2_FILE"), extensionFeature, @"..\ClassFoo\bin\" + BUILD + @"\ClassFoo.dll"))
                );

            project.DefaultFeature.Add(binariesFeature)
                .Add(extensionFeature);

            project.Properties.Add(new Property("FEATURE1_INSTALL_PATH"))
                .Add(new Property("FEATURE2_INSTALL_PATH"));

            project.Version = new Version("1.0.0");
            project.ControlPanelInfo.Manufacturer = "My Company";
            project.ControlPanelInfo.Contact = "Tim Cartwright";
            project.GUID = new Guid("11C8BE07-ACF9-4172-B569-BBD324B597A6");
            project.MajorUpgradeStrategy = MajorUpgradeStrategy.Default;
            //project.LicenceFile = ""; //TODO: SET THE LICENSE FILE

            project.ManagedUI = ManagedUI.Empty;    //no standard UI dialogs
            project.ManagedUI = ManagedUI.Default;  //all standard UI dialogs
            //custom set of standard UI dialogs
            project.ManagedUI = new ManagedUI();
            project.ManagedUI.InstallDialogs.Add(Dialogs.Welcome)
                                            .Add(Dialogs.Licence)
                                            .Add(Dialogs.SetupType)
                                            .Add(Dialogs.Features)
                                            //.Add(Dialogs.InstallDir)
                                            .Add(Dialogs.Progress)
                                            .Add(Dialogs.Exit);

            project.ManagedUI.ModifyDialogs.Add(Dialogs.MaintenanceType)
                                           .Add(Dialogs.Features)
                                           .Add(Dialogs.Progress)
                                           .Add(Dialogs.Exit);

            //project.Load += Msi_Load;
            //project.BeforeInstall += Msi_BeforeInstall;
            //project.AfterInstall += Msi_AfterInstall;
            project.UILoaded += Msi_UILoaded;
            //project.SourceBaseDir = "<input dir path>";
            project.OutDir = "Installer";

            project.BuildMsi();

            //DO NOT DO THIS AS IT WILL CAUSE A BUILD EXCEPTION
            //Console.WriteLine("Press any key to continue.");
            //Console.ReadKey(true);
        }

        private static void Msi_UILoaded(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                try
                {
                    //IS THIS WHERE I CAN SET THE DIRECTORIES????
                    if (e.IsInstalling)
                    {
                        e.Session["FEATURE1_INSTALL_PATH"] = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Microsoft\MSEnvShared\Addins");

                        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\AppEnv.0\Apps\ssms_13.0", false))
                        {
                            var path = key.GetValue("StubExePath") as string;
                            if (!string.IsNullOrEmpty(path))
                            {
                                path = IO.Path.Combine(IO.Path.GetDirectoryName(path), @"Extensions\My Company Test Product");
                                //IO.Directory.CreateDirectory(path);
                                e.Session["FEATURE2_INSTALL_PATH"] = path;
                            }
                            key.Close();
                        }

                        //MessageBox.Show("FEATURE1_INSTALL_PATH = " + e.Session["FEATURE1_INSTALL_PATH"]);
                        //MessageBox.Show("FEATURE2_INSTALL_PATH = " + e.Session["FEATURE2_INSTALL_PATH"]);
                        //MessageBox.Show(e.ToString(), "UILoaded");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        static void Msi_Load(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "Load");
            }
        }

        static void Msi_BeforeInstall(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "BeforeInstall");
            }
        }

        static void Msi_AfterInstall(SetupEventArgs e)
        {
            //if (!e.IsUISupressed && !e.IsUninstalling)
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "AfterExecute");
            }
        }
    }

    //public class CustomActions
    //{
    //    [CustomAction]
    //    public static ActionResult SetInstallPaths(Session session)
    //    {
    //        MessageBox.Show("Hello World!", "Embedded Managed CA");
    //        session.Log("Begin MyAction Hello World");

    //        return ActionResult.Success;
    //    }
    //}
}

我终于明白了。我没有意识到 IDS 可以像属性一样在会话中设置。

主要变化发生在 UILoaded 事件中,我没有设置属性,而是像这样设置目录对象本身的 ID:

e.Session["FEATURE1_INSTALL_PATH"] = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Microsoft\MSEnvShared\Addins");

这是更新后的代码:

using System;
using System.Linq;
using IO = System.IO;
using System.Windows.Forms;
using WixSharp;
using WixSharp.Forms;
using Microsoft.Win32;
using Microsoft.Deployment.WindowsInstaller;
using System.Diagnostics;

namespace WixSharpSetup
{
    class Program
    {

#if DEBUG
        const string BUILD = "Debug";
#else
        const string BUILD = "Release";
#endif

        static void Main()
        {

            var binariesFeature = new Feature("Feature 1", "Feature 1");
            var extensionFeature = new Feature("Feature 2", "Feature 2");

            var project = new ManagedProject("My Company Test Product",
                //new ManagedAction(CustomActions.SetInstallPaths, Return.ignore, When.Before, Step.InstallInitialize, Condition.NOT_Installed),
                new Dir(new Id("MAIN_INSTALL_PATH"), binariesFeature, @"%ProgramFiles%\My Company\Test Product",
                    new File(new Id("FEATURE1_FILE1"), binariesFeature, @"..\ClassFoo\bin\" + BUILD + @"\ClassFoo.dll"))
/* **** THIS DIRECTORY NEEDS TO BE SET PROGRAMATICALLY TO THE FEATURE1_INSTALL_PATH *****/
                , new Dir(new Id("FEATURE1_INSTALL_PATH"), binariesFeature, "NOT_SET",
                    new File(new Id("FEATURE1_FILE2"), binariesFeature, @"..\ClassFoo\bin\" + BUILD + @"\test extra file.txt"))
/* **** THIS DIRECTORY NEEDS TO BE SET PROGRAMATICALLY TO THE FEATURE2_INSTALL_PATH *****/
                , new Dir(new Id("FEATURE2_INSTALL_PATH"), extensionFeature, @"NOT_SET",
                    new File(new Id("FEATURE2_FILE"), extensionFeature, @"..\ClassFoo\bin\" + BUILD + @"\ClassFoo.dll"))
                );

            project.DefaultFeature.Add(binariesFeature)
                .Add(extensionFeature);

            //project.Properties.Add(new Property("FEATURE1_INSTALL_PATH"))
            //    .Add(new Property("FEATURE2_INSTALL_PATH"));

            project.Version = new Version("1.0.0");
            project.ControlPanelInfo.Manufacturer = "My Company";
            project.ControlPanelInfo.Contact = "Tim Cartwright";
            project.GUID = new Guid("11C8BE07-ACF9-4172-B569-BBD324B597A6");
            project.MajorUpgradeStrategy = MajorUpgradeStrategy.Default;
            //project.LicenceFile = ""; //TODO: SET THE LICENSE FILE

            project.ManagedUI = ManagedUI.Empty;    //no standard UI dialogs
            project.ManagedUI = ManagedUI.Default;  //all standard UI dialogs
            //custom set of standard UI dialogs
            project.ManagedUI = new ManagedUI();
            project.ManagedUI.InstallDialogs.Add(Dialogs.Welcome)
                                            .Add(Dialogs.Licence)
                                            .Add(Dialogs.SetupType)
                                            .Add(Dialogs.Features)
                                            //.Add(Dialogs.InstallDir)
                                            .Add(Dialogs.Progress)
                                            .Add(Dialogs.Exit);

            project.ManagedUI.ModifyDialogs.Add(Dialogs.MaintenanceType)
                                           .Add(Dialogs.Features)
                                           .Add(Dialogs.Progress)
                                           .Add(Dialogs.Exit);

            //project.Load += Msi_Load;
            //project.BeforeInstall += Msi_BeforeInstall;
            //project.AfterInstall += Msi_AfterInstall;
            project.UILoaded += Msi_UILoaded;
            //project.SourceBaseDir = "<input dir path>";
            project.OutDir = "Installer";

            project.BuildMsi();

            //DO NOT DO THIS AS IT WILL CAUSE A BUILD EXCEPTION
            //Console.WriteLine("Press any key to continue.");
            //Console.ReadKey(true);
        }

        private static void Msi_UILoaded(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                try
                {
                    //IS THIS WHERE I CAN SET THE DIRECTORIES????
                    if (e.IsInstalling)
                    {
                        e.Session["FEATURE1_INSTALL_PATH"] = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Microsoft\MSEnvShared\Addins");

                        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\AppEnv.0\Apps\ssms_13.0", false))
                        {
                            var path = key.GetValue("StubExePath") as string;
                            if (!string.IsNullOrEmpty(path))
                            {
                                path = IO.Path.Combine(IO.Path.GetDirectoryName(path), @"Extensions\My Company Test Product");
                                //IO.Directory.CreateDirectory(path);
                                e.Session["FEATURE2_INSTALL_PATH"] = path;
                            }
                            key.Close();
                        }

                        //MessageBox.Show("FEATURE1_INSTALL_PATH = " + e.Session["FEATURE1_INSTALL_PATH"]);
                        //MessageBox.Show("FEATURE2_INSTALL_PATH = " + e.Session["FEATURE2_INSTALL_PATH"]);
                        //MessageBox.Show(e.ToString(), "UILoaded");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        static void Msi_Load(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "Load");
            }
        }

        static void Msi_BeforeInstall(SetupEventArgs e)
        {
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "BeforeInstall");
            }
        }

        static void Msi_AfterInstall(SetupEventArgs e)
        {
            //if (!e.IsUISupressed && !e.IsUninstalling)
            if (e.IsInstalling)
            {
                MessageBox.Show(e.ToString(), "AfterExecute");
            }
        }
    }

    //public class CustomActions
    //{
    //    [CustomAction]
    //    public static ActionResult SetInstallPaths(Session session)
    //    {
    //        MessageBox.Show("Hello World!", "Embedded Managed CA");
    //        session.Log("Begin MyAction Hello World");

    //        return ActionResult.Success;
    //    }
    //}
}