简体   繁体   中英

Wix- How to copy a directory to install folder

I have a Bin folder with many files in my project. For now, I only know how to add specific files to install folder with the code below.

<File Source='$(var.Debug)Myapplication.exe' Id='file4028276A0CAD84E3BDE449237AE31A5F' />  

However, I want to move a whole directory to the install path. For example, move the whole folder "Bin" to the install path "C:\\Myapplication".

What should I do then?

Thanks in advance!

Sounds like what you want to use is the WiX tool heat , which is used to "harvest" a directory (or individual files) and create a WiX fragment file that you can use in your project.

For example, you have a directory you want to harvest, and it may include subdirectories, but there's a lot of files, and you want it all... heat will do that for you.

Consider this trivial structure:

Somedir
    |
    |---A file.txt
    |---An init file.ini
    |---another file.txt
    |---subfolder
            |
            |---a subfolder file.txt

If you use heat to harvest this directory, the tool will generate the fragment file for you that you can use as a component reference in your project without having to specify the files one at a time or use a workaround solution.

For example, the following heat command will process this directory (from one level up in this example)

heat dir somedir -o MyHarvestedStuff.wxs -scom -frag -srd -sreg -gg -cg MyComponentGroupId -dr BIN_DIR_REF

Where:

dir = harvest a directory
somedir = directory you want to harvest
-o MyHarvestedStuff.wxs = the output fragment file
-scom -sfrag -srd -sreg = Suppress COM elements, fragments, root directory as element, registry harvesting (these options will create a grouping that most applications can use)
-gg = generate GUID's now (this will put the GUID's into your output file, rather than using a wildcard "*". The advantage here is you can use non-standard TARGETDIR, which would not qualify for autogenerated GUID's)
-cg MyComponentGroupId = component group. this is what you will use in your feature set to include these files
-dr BIN_DIR_REF = this is the directory reference for which to place the files in your final package.

The resulting XML looks like this (this was run without -gg to avoid posting real GUID's)

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="BIN_DIR_REF">
            <Directory Id="dirF065D7446868E03DB0B296EBADA4E4A1" Name="subfolder" />
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="MyComponentGroupId">
            <Component Id="cmp739547000B47E975B0452D876AF7810B" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="fil09B311A6D1EABD9E94DFA5C83F59C548" KeyPath="yes" Source="SourceDir\A file.txt" />
            </Component>
            <Component Id="cmp84C8400F892D39B05EE3021CCEEAA14F" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="fil11A22646343997D26AC54171A62DFF4C" KeyPath="yes" Source="SourceDir\an init file.ini" />
            </Component>
            <Component Id="cmpFA266FC6F3269CB5D9E42C38FC995117" Directory="BIN_DIR_REF" Guid="PUT-GUID-HERE">
                <File Id="filA545B6E4B63B8211E982917FC78F6EB4" KeyPath="yes" Source="SourceDir\another file.txt" />
            </Component>
            <Component Id="cmp2EC5C1618A59F47B7BDE800EB9AA8688" Directory="dirF065D7446868E03DB0B296EBADA4E4A1" Guid="PUT-GUID-HERE">
                <File Id="filB0CD0B02385137DC806112E293083459" KeyPath="yes" Source="SourceDir\subfolder\a subfolder file.txt" />
            </Component>
        </ComponentGroup>
    </Fragment>
</Wix>

In your project file, you'd have something like this under your root <Directory> element:

<Directory Id="BIN_DIR_REF" Name="bin">
    <!-- anything else you might put here...-->
</Directory>

And in your feature group:

<Feature Id="Complete" Level="1">
    ...
    <ComponentGroupRef Id="MyComponentGroupId" />         
    ...
</Feature>

Tying it all together...

  1. Supply your new fragment file to candle along with your other file(s)
  2. Supply the .wixobj file that candle produces to light
  3. Resolve the SourceDir reference in your new fragment file with a WiX preprocessor variable or with the -b option to light

    Ex: light ... -b "path to my directory that I harvested" ...

Don't let the length of the answer deter you from exploring this solution, it works quite well and it's pretty simple. Now if you want to exclude anything from that directory, that's a whole other story...

how to include a large directory tree in a wix installer

This looks good. But too much work to do.

How do copy a folder in wix

The above link worked fine. But cannot copy sub folders.

So what I did was, I created an app which reads the whole folder and get its sub directories and files and generates the necessary code blocks like

 <Component Id="myfile" Guid="GUID">
     <File Id="myfile.txt" Source="MySourceFiles\myfile.txt" KeyPath="yes" Checksum="yes"/>
 </Component>

Then I added the generated code block to the .wxs file.

The initial time you spend for creating this app is not a waste. Because you can keep using it forever. I recommend you to copy file by file, because it's helpful during an upgrade or a repair. MSI keeps a record for each file which is to be copied. So it's maintainable and useful on upgrades and repairs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM