我想用wix#动态设置一个特性目录路径

本文关键字:一个 路径 wix# 动态 设置 | 更新日期: 2023-09-27 18:02:09

我试图动态设置在wix#中创建的wix项目的路径。目录应由用户配置。其中两个目录应该在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'14.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;
    //    }
    //}
}

我想用wix#动态设置一个特性目录路径

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

主要的变化发生在uilloaded事件中,我没有设置属性,而是设置了目录对象本身的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'14.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;
    //    }
    //}
}