简体   繁体   中英

Wix Installer - Create Folder hierarchy based on Property

I am using Wix 3.6 to create a setup. I am still learning as I go along. The information out there is still scattered around. I am just waiting for my Wix Developer Guide book arriving.

I currently have a custom UI dialog where the user enters some application configuration. Part of that configuration is to specify a log folder. This at present this just sets a property [LogFolder]. This is defaulted to something like D:\\Logs.

I want the installer to create that directory when the setup is run. I have the following to try and do this but it just created a folder named [LOGFOLDER] on the D: drive when I run the setup.

<Product ...
    <Directory Id="TARGETDIR" Name="SourceDir" >
        <Directory Id="LogFolderDir" Name="[LOGFOLDER]" >
            <Component Id="LogFolderComponent" Guid="{7E7D6916-B321-40D6-ABAD-696B57A6E5FB}" KeyPath="yes">
                <CreateFolder />
            </Component>
        </Directory>
    </Directory>
    ...
</Product>

How can I do this with Wix?

The first step is create a property set to the value you want:

<Product>
  <Property Id="LOGFOLDER" Value="D:\Logs" />
</Product>

The second step is to create a dialog where you set this property (or another thing to change its value):

<Dialog>
  <Control Id="Edit_LogFolder" Type="Edit" Property="LOGFOLDER" />
</Dialog>

Then you need to change your directories structure to create this folder in a default location:

<Directory Id="ProgramFilesFolder">
  <Directory Id="INSTALLFOLDER" Name="MyApp">

    <Directory Id="LOGFOLDER" Name="Logs" />

  </Directory>
</Directory>

The last step is to create a Component that will create the directory, like this:

<ComponentGroup Id="ComponentGroup_LogFolder">
  <Component Id="Component_LogFolder" Guid="" Directory="LOGFOLDER">

    <CreateFolder Directory="LOGFOLDER" />

  </Component>
</ComponentGroup>

Remark:

If D:\\ is a disc drive and you have a disc inserted, the installation will fail because it will try to create the folder and it won't succeed.

The Name attribute isn't formattable so you can use properties in it. The Id 'LogFolderDir' doesn't have a parent such as "ProgramFilesFolder' so it's defaulting to the volume with the largest amount of disk space. In this case D but YMMV.

It's dangerous to default to D: because D: might not exist. How I'd set this directory up is Id="LOGDIR" Name="Logs" and make it a child of the INSTALLDIR/INSTALLLOCATION directory element. Then in your custom UI, wire up another BrowseFolder dialog to give the user the ability to override it. Or, make it associated with a required Logs feature so that the stock feature selection dialog can be used to select the feature and browse the destination folder.

If you still want it to "default" to D:\\Logs what I would do is have a custom action that checks to see if D: exists and is a fixed disk. If so, set the LOGDIR=D:\\Logs

There is a simpler solution by using the same ID for the property and the directory (without naming the directory). But you must use the full path of the folder in the property.

Let's say the log directory is C: \\ ProgramDirectory \\ Data \\ Log and you want to set Data with a property (usually if the value of Data is different and conditionally set).

<Property Id="PR_DATA_DIRECTORY" Value="C:\ProgramDirectory\Data" />
<Directory Id="TARGETDIR" Name="SourceDir">
    <Directory Id="DIR_PROGRAM" Name="C:\ProgramDirectory" >
        <Directory Id="PR_DATA_DIRECTORY">
            <Directory Id="DIR_LOG" Name="Log" />
        <Directory/>
    </Directory>
</Directory>

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