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

时间:2016-08-12 22:18:18

标签: c# wix wixsharp

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

我还应该声明我是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;
    //    }
    //}
}

1 个答案:

答案 0 :(得分:0)

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

主要的变化发生在UILoaded事件中,而不是设置属性,我设置了目录对象本身的ID,如下所示:

testSetPredictions = predict(Mdl.Trained{k}, testSetFeatures);

以下是更新后的代码:

sub("\\s*[^0-9]+$", "", vector)
#[1] "May 20" "Dez 1"  "Oct 12"