Files in InstallDir

Jan 18, 2016 at 6:26 PM
Hi,

I have a project where I am setting the InstallDir property using a ManagedAction.
This is working fine.
But how do I add files to the InstallDir folder?
This should be easy, but I haven't managed to get it right:
var msiProject = new Project("My App",
                new Dir(@"[INSTALLDIR]",
                new File(@"ClientFiles\MyClient.exe")));
msiProject.Actions.Add(new ManagedAction("SetInstalldir",
                        Return.check,
                        When.Before,
                        Step.InstallFiles,
                        Condition.Always));
Best regards
Flemming
Jan 19, 2016 at 1:15 AM
Edited Jan 19, 2016 at 1:15 AM
The code you provided will not produce a proper MSI.

The correct Wix# project definition should look something like that:
var project =
    new Project("MyProduct",
        new Dir(@"%ProgramFiles%\My Company\My Product",
            new File(@"Files\Bin\MyApp.exe"),
            new Dir(@"Docs\Manual",
                new File(@"Files\Docs\Manual.txt") )));

project.UI = WUI.WixUI_InstallDir;
project.GUID = new Guid("6f330b47-2577-43ad-9095-1861ba25889b");

project.BuildMsi();
Probably it is the best to start with the samples included in the downloadables (e.g. 'Install Files').
Jan 19, 2016 at 7:25 AM
Thanks, but you misunderstand me.
I am changing the InstallDir using a custom actions and just wants to add my file to whatever the InstallDir is set to.
In the examples I have seen, the initial folder is set manual like:
new Dir(@"%ProgramFiles%\My Company\My App"
I don't want to put any files in program files or other "manually" specified folder, but just in InstallDir - or subfolders to that.
The following code works, but put the file in %programfiles%\My Company\My App.
        class Script
        {
            static public void Main()
            {
                try
                {
                    var msiProject = new Project("My App",
                        new Dir(@"%ProgramFiles%\My Company\My App",
                        new File(@"Files\myapp.exe")));
                    msiProject.Actions.Add(new ManagedAction("SetInstalldir",
                                        Return.check,
                                        When.Before,
                                        Step.InstallFiles,
                                        Condition.Always));
                    msiProject.Version = new Version(1, 0, DateTime.Now.Year, DateTime.Now.Month);
                    msiProject.Platform = Platform.x64;
                    msiProject.OutFileName = "MyApp " + msiProject.Platform.ToString();
                    msiProject.GUID = new Guid("B5C99E99-39E8-4417-9391-14B3E28E1432");
                    msiProject.SetNetFxPrerequisite("NETFRAMEWORK40FULL='#1'");
                    Compiler.PreserveTempFiles = true;
                    Compiler.BuildMsi(msiProject);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        public class CustomActions
        {
            [CustomAction]
            public static ActionResult SetInstalldir(Session session)
            {
                //get the help installation folder
                session.Log("Begin CustomAction SetInstalldir");

                RegistryKey consoleKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Help\\v1.0");
                if (consoleKey != null)
                {
                    string consolefolder = Convert.ToString(consoleKey.GetValue("AppRoot", string.Empty));
                    session.Log("Setting INSTALLDIR to Help folder '" + consolefolder + "'");
                    session["INSTALLDIR"] = consolefolder;
                }
                session.Log("End CustomAction SetInstalldir");
                return ActionResult.Success;
            }
        }
So I am looking for the variable for [INSTALLDIR] to set in new Dir(--->[INSTALLDIR]<---- ??

Best regards
Flemming
Jan 19, 2016 at 11:33 PM
Edited Jan 20, 2016 at 4:57 AM
The problem with your solution is that you set the INSTALLDIR too late. MSI runtime already has been instructed to install files into '%ProgramFiles%'. If you want to fix it you will need to schedule your custom action for some earlier stage (not "before InstallFiles").

Actually ManagedSetup allows you do it in a more natural way:
static void Main()
{
    var project = new ManagedProject("MyProduct",
                        new Dir(@"%ProgramFiles%\My Company\My Product",
                            new File("Program.cs")));

    project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889b");
    project.UI = WUI.WixUI_ProgressOnly;

    project.Load += (e) =>
                    {
                        if (e.IsInstalling)
                            e.Session["INSTALLDIR"] = @"C:\Temp\test_dir";
                    };
    project.BuildMsi();
}
Note, Load event triggers execution of a custom action (event handler) before AppSearch standard action.
Marked as answer by pilbro on 1/21/2016 at 12:35 AM
Jan 21, 2016 at 8:41 AM
Aha, that worked!
However, I needed to set TargetDir as well.
So for others, the following example works. The %ProgramFiles% will be ignored, the file will be paced in InstallDir/TargetDir and the sub directories placed accordingly.
            static public void Main()
            {
                try
                {
                    var msiProject = new ManagedProject("My App",
                        new Dir(@"%ProgramFiles%\My Company\My Product", new File(@"Files\myapp.exe")),
                        new Dir("SubDirRoot", new File(@"Files\myapp.exe"),
                        new Dir("SubDirLevel1", new File(@"Files\myapp.exe"))),
                        new Dir("SubDirRoot2", new File(@"Files\myapp.exe")));
                    msiProject.Version = new Version(1, 0, DateTime.Now.Year, DateTime.Now.Month);
                    msiProject.Platform = Platform.x64;
                    msiProject.OutFileName = "MyApp " + msiProject.Platform.ToString();
                    msiProject.GUID = new Guid("B5C99E99-39E8-4417-9391-14B3E28E1432");
                    msiProject.SetNetFxPrerequisite("NETFRAMEWORK40FULL='#1'");
                    msiProject.Load += (e) =>
                    {
                        if (e.IsInstalling)
                        {
                            e.Session.Log("Begin WIXSharp CustomAction SetInstalldir");
                            RegistryKey consoleKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Help\\v1.0");
                            if (consoleKey != null)
                            {
                                string consolefolder = Convert.ToString(consoleKey.GetValue("AppRoot", string.Empty));
                                e.Session.Log("Setting INSTALLDIR to Help folder '" + consolefolder + "'");
                                e.Session["INSTALLDIR"] = consolefolder;
                                e.Session["TARGETDIR"] = consolefolder;
                            }
                            e.Session.Log("End WixSharp CustomAction SetInstalldir");
                        }
                    };
                    Compiler.PreserveTempFiles = true;
                    Compiler.BuildMsi(msiProject);
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
Best regards
Flemming