Wix toolset: create directory in root disk (system

2019-02-04 10:40发布

问题:

I'm aware of similar questions inside stackoverflow:

WIX:default directory in WixUI_InstallDir,

WIX installer root directory and versioning,

Is it possible to have two root directories in WIX,

copy file to custom dir in another partition,

How to create a directory in wix?

however none of them shows a simple and immediate code to create a folder inside the C:\ folder (not hard coded but should be the root disk or system disk or whatever you would call the disk which contains the Windows folder) and to copy files inside it.

In other words how can Wix create a C:\MynewDir\example.jar folder?

Here's what I tried:

<?xml version="1.0" encoding="UTF-8"?>
<!-- WiX installer MyProgram by Mark Seuffert -->
<?define ProductVersion = "13.1.2.3"?>
<?define ProductUpgradeCode = "12345678-1234-1234-1234-111111111112"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" UpgradeCode="$(var.ProductUpgradeCode)" Name="MyProgram" Version="$(var.ProductVersion)" Manufacturer="COMPANY" Language="1033">
        <Package InstallerVersion="200" Compressed="yes" Comments="Windows Installer Package" />
        <Media Id="1" Cabinet="product.cab" EmbedCab="yes" />

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLDIR" Name="MyProgram">
                    <Component Id="ApplicationFiles" Guid="12345678-1234-1234-1234-222222222222">
                        <File Id="ApplicationFile1" Source="C:\Users\user\Desktop\myprogram.exe" />
                    </Component>
                </Directory>
            </Directory>

            <Directory Id="ANOTHERLOCATION" FileSource="C:\MynewDir">
            </Directory> 
        </Directory>

        <DirectoryRef Id="ANOTHERLOCATION" FileSource="C:\MynewDir">
          <Component Id="ApplicationFiles2" Guid="12345678-1234-1234-1235-111111111111">
                    <File Id="ApplicationFile2" Source="C:\Users\user\Desktop\InstallerFiles_13_4_9_3\myprogramLauncher.jar" />
                    <CreateFolder />
            </Component>
        </DirectoryRef>

        <InstallExecuteSequence>
            <RemoveExistingProducts After="InstallValidate" />
        </InstallExecuteSequence>
        <Feature Id="DefaultFeature" Level="1">
            <ComponentRef Id="ApplicationFiles2" />
            <ComponentRef Id="ApplicationFiles" />
        </Feature>
    </Product>
</Wix>

EDIT 1: Yan Sklyarenko just found what I was looking for, that's the WindowsVolume (I don't know how I missed it inside http://msdn.microsoft.com/en-us/library/windows/desktop/aa370905%28v=vs.85%29.aspx#system_folder_properties microsoft document).

However how do I replace FileSource="C:\MynewDir" with FileSource="[WindowsVolume]MynewDir" ??? because apparently even with WINDOWSVOLUME the resulting volume chosen is always D:\ in my computer which has more available space :(

EDIT 2 I updated my code using Yan Sklyarenko's second sample ( @@@@newpart@@@@ identifies parts where code differs), however behaviour is still the same, the installer chooses the disk with more free space (D:\ in my case) and not C:\ where windows is..

<?xml version="1.0" encoding="UTF-8"?>
<!-- WiX installer MyProgram by Mark Seuffert -->
<?define ProductVersion = "13.1.2.3"?>
<?define ProductUpgradeCode = "12345678-1234-1234-1234-111111111112"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" UpgradeCode="$(var.ProductUpgradeCode)" Name="MyProgram" Version="$(var.ProductVersion)" Manufacturer="COMPANY" Language="1033">
        <Package InstallerVersion="200" Compressed="yes" Comments="Windows Installer Package" />
        <Media Id="1" Cabinet="product.cab" EmbedCab="yes" />

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLDIR" Name="MyProgram">
                    <Component Id="ApplicationFiles" Guid="12345678-1234-1234-1234-222222222222">
                        <File Id="ApplicationFile1" Source="C:\Users\user\Desktop\myprogram.exe" />
                    </Component>
                </Directory>
            </Directory>

            <Directory Id="ANOTHERLOCATION" FileSource="C:\MynewDir">
             @@@@newpart@@@@<Component Id="ApplicationFiles2" Guid="12345678-1234-1234-1235-111111111111">
                    <File Id="ApplicationFile2" Source="C:\Users\user\Desktop\InstallerFiles_13_4_9_3\myprogramLauncher.jar" />
                    <CreateFolder />
               </Component>
            </Directory> 
        </Directory>

        @@@@newpart@@@@<SetDirectory Id="ANOTHERLOCATION" Value="[WINDOWSVOLUME]" />

        <InstallExecuteSequence>
            <RemoveExistingProducts After="InstallValidate" />
        </InstallExecuteSequence>
        <Feature Id="DefaultFeature" Level="1">
            <ComponentRef Id="ApplicationFiles2" />
            <ComponentRef Id="ApplicationFiles" />
        </Feature>
    </Product>
</Wix>

EDIT 3 The last code snippet above should work however change the casing of WINDOWSVOLUME to WindowsVolume as suggested.

回答1:

Here is a complete working solution based on your code simplified (notice the comment in the code):

<?define ProductVersion = "13.1.2.3"?>
<?define ProductUpgradeCode = "12345678-1234-1234-1234-111111111112"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" UpgradeCode="$(var.ProductUpgradeCode)" Name="MyProgram"
         Version="$(var.ProductVersion)" Manufacturer="COMPANY" Language="1033">
    <Package InstallerVersion="200" Compressed="yes" />
    <Media Id="1" Cabinet="product.cab" EmbedCab="yes" />

    <Directory Id="TARGETDIR" Name="SourceDir">
        <Directory Id="ProgramFilesFolder">
            <Directory Id="INSTALLDIR" Name="MyProgram" />
           <Directory Id="ANOTHERLOCATION" />
        </Directory>
    </Directory>

    <!-- The casing of 'ANOTHERLOCATION' and 'WindowsVolume' is very important here.
         Replace 'MyNewDir' with the correct name of the folder you want on
         WindowsVolume.
    -->
    <SetDirectory Id="ANOTHERLOCATION" Value="[WindowsVolume]MyNewDir" />


    <Feature Id="DefaultFeature" Level="1">
      <Component Directory="INSTALLDIR">
        <File Id="ApplicationFile1" Source="C:\Users\user\Desktop\myprogram.exe" />
      </Component>
      <Component Directory="ANOTHERLOCATION">
        <File Id="ApplicationFile2" Source="C:\Users\user\Desktop\InstallerFiles_13_4_9_3\myprogramLauncher.jar" />
      </Component>
    </Feature>
</Product>
</Wix>


回答2:

Ok, you can do something like this:

<Directory Id="TARGETDIR" Name="SourceDir">
  <Directory Id="WindowsVolume">
    <Directory Id="MyNewDirId" Name="MyNewDir">
      <Component Id="SampleComponent" Guid="...">
        <File Id="SampleFile" Source="..." KeyPath="yes" />
      </Component>
    </Directory>
  </Directory>
</Directory>

This will install the file to the MyNewDir folder on Windows drive (C: in my case). However, it will complain that using WindowsVolume in this fashion might have unexpected side effects.

To satisfy that validation, you can change the sample to:

<Directory Id="TARGETDIR" Name="SourceDir">
  <Directory Id="MyNewDirId" Name="MyNewDir">
    <Component Id="SampleComponent" Guid="...">
      <File Id="SampleFile" Source="..." KeyPath="yes" />
    </Component>
  </Directory>
</Directory>

<SetDirectory Id="MyNewDirId" Value="[WindowsVolume]MyNewDir" />

This looks more like a hack, but the result is the same. To be honest, I don't understand what those "unexpected side effects" could be. Maybe, Windows Installer gurus can shed some light on this.

Hope this helps.