0

The snippet of Files.wxs generated by heat:

<?xml version="1.0" encoding="utf-8"?>
<Wix
xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="INSTALLFOLDER">
            <Directory Id="dirF5D9BDF13CBC346EDDFD6D0959FFB838" Name="config">
                <Component Id="cmp0CBEDCE6B62E5666B3362D0EB41267BC" Guid="*">
                    <File Id="fil73D1987B7864F07C97735D7E40243AB2" KeyPath="yes"
Source="$(var.App.TargetDir)\config\accounts-example.ini" />
                </Component>
                </Component>
            </Directory>
        </DirectoryRef>
    </Fragment>
    <Fragment>
        <ComponentGroup Id="Binaries">
            <ComponentRef Id="cmp0CBEDCE6B62E5666B3362D0EB41267BC" />
        </ComponentGroup>
    </Fragment>
</Wix>

Product.wxs:

<?xml version="1.0" encoding="utf-8"?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">
  <Product Id="*" Name="xxx" Language="2052" Version="$(var.ProductVersion)" Manufacturer="xxx"
           UpgradeCode="425BDA6F-31B8-47AD-88D8-4B2DBE394XXX">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

    <MajorUpgrade AllowSameVersionUpgrades="yes" DowngradeErrorMessage="New Version [ProductName] has been installed。" />
    <MediaTemplate EmbedCab="yes" />

    <WixVariable Id="WixUILicenseRtf" Value="./License.rtf" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="MANUFACTURERFOLDER" Name="!(bind.property.Manufacturer)">
          <Directory Id="INSTALLFOLDER" Name="!(bind.property.ProductName)" />
        </Directory>
      </Directory>
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="!(bind.property.ProductName)" />
        <Directory Id="DesktopFolder" Name="Desktop" />
      </Directory>
    </Directory>

    <Feature Id="ProductFeature" Title="XXX_Installer" Level="1">
      <ComponentGroupRef Id="Binaries" />
      <ComponentRef Id="ApplicationShortcut" />
      <ComponentRef Id="ApplicationShortcutDesktop" />
      <ComponentRef Id="RegistryEntries" />
    </Feature>

    <UIRef Id="WixUI_ErrorProgressText" />
    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />
    <UIRef Id="WixUI_InstallDir" />

    <DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationShortcut" Guid="E1F61345-CC60-40FE-8FC4-FBE1598F8XXX">
        <Shortcut Id="ApplicationStartMenuShortcut"
                  Name="!(bind.property.ProductName)"
                  Description="!(bind.property.ProductName)"
                  Target="[INSTALLFOLDER]XXX_App.exe"
                  WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\Microsoft\!(bind.property.ProductName)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="DesktopFolder">
      <Component Id="ApplicationShortcutDesktop" Guid="BEDF111F-0889-4317-8E67-41425F00CXXX">
        <Shortcut Id="ApplicationDesktopShortcut"
                  Name="!(bind.property.ProductName)"
                  Description="!(bind.property.ProductName)"
                  Target="[INSTALLFOLDER]XXX_App.exe"
                  WorkingDirectory="INSTALLFOLDER" />
        <RemoveFolder Id="DesktopFolder" On="uninstall" />
        <RegistryValue Root="HKCU" Key="Software\Microsoft\!(bind.property.ProductName)" Name="installed" Type="integer" Value="1" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="TARGETDIR">
      <Component Id="RegistryEntries" Guid="0D919675-E219-43EA-AAB3-E6F81A013XXX">
        <RegistryKey Root="HKCU"
                     Key="Software\Microsoft\Windows\CurrentVersion\Run">
          <RegistryValue Name="!(bind.property.ProductName)"
                         Type="string"
                         Value="[INSTALLFOLDER]XXX_App.exe"/>
        </RegistryKey>
      </Component>
    </DirectoryRef>

    <UI>
      <Publish Dialog="ExitDialog"
          Control="Finish"
          Event="DoAction"
          Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
    </UI>
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="启动!(bind.property.ProductName)" />
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1" />

    <Property Id="WixShellExecTarget" Value="[INSTALLFOLDER]XXX_App.exe" />
    <CustomAction Id="LaunchApplication"
        BinaryKey="WixCA"
        DllEntry="WixShellExec"
        Impersonate="yes" />
  </Product>
</Wix>

Every time I install a new version, it overwrites the existing file accounts-example.ini.

The overwrite behavior is expected for accounts-example.ini, but I will have another file user.db do not want to be overwritten.

It says if KeyPath is set to 'yes' then it doesn't overwrite the existing file, isn't it? How could I config Wix to overwrite one file and not overwirte another?


Some articles about KeyPath:

what-is-the-wix-keypath-attribute

copy-if-not-exist-in-wix

Community
  • 1
  • 1
user1633272
  • 2,007
  • 5
  • 25
  • 48

1 Answers1

1

If you are doing this with a majorupgrade element you need to tell us where it's scheduled. If it's "early" (such as afterInstallInitialize) then everything will be uninstalled first, and then the new upgrade installed, and every file that was installed will be from the new upgrade. If it's sequenced "late" (such as afterInstallExecute) then the overwrite rules apply (such as this https://msdn.microsoft.com/en-us/library/windows/desktop) and that's because the upgrade basically installs each file over the existing files.

Note that the WiX default for the MajorUpgrade schedule is afterInstallValidate, so as I described (and as the WiX documentation says) the entire older product will be uninstalled first (obviously removing all files) then all the new files will be installed.

See schedule here:

http://wixtoolset.org/documentation/manual/v3/xsd/wix/majorupgrade.html

The explanation at the WiX keypath link leaves a lot to be desired. It is not really true to say that if the component is present none of its resources will be installed. It's not clear to me what that is intended to mean because the overwrite rules will be applied.

As far as your data files are concerned, they will not be overwritten by an incoming file if they have been updated since they were installed. So it's likely that your ini file is being overwritten because it hasn't been changed. If your db has been updated then it won't be replaced, but, again, this is overwrite rules when your major upgrade is "late" (or it's a patch).

PhilDW
  • 20,260
  • 1
  • 18
  • 28
  • I just added the Product.wxs in topic. I changed the installed file and then reinstalled installer, the file got overwritten. – user1633272 Mar 20 '17 at 16:41
  • See my expanded answer. It's not actually "overwriting" the file. You've just got the major upgrade scheduled to first remove all the files and then install the new ones. It's as if you manually uninstalled the old product then installed your new one. – PhilDW Mar 20 '17 at 18:50
  • How to avoid this? I mean overwrite one file but not for another. – user1633272 Mar 21 '17 at 14:05
  • The upgrade should be scheduled after Installexecute, then overwrite rules apply. As the rules state, a data file will not be overwritten if it has been updated since it was installed, so a modified DB or ini file will not be overwritten. Has the ini file been updated since install? Do you use file hash for data files based on the MSI file hash? It's not clear if you want to overwrite the ini file because you think it's the right thing to do, even if your upgrade ini file is identical. – PhilDW Mar 21 '17 at 18:28
  • Thanks. I don't not really overwrite the ini file, just overwrite xxx-template.ini, the actual ini is initially generated from the template.ini. I'm a newbie to WiX, so don't know how to 'schedule upgrade after Installexecute'. Any link or document can I read? – user1633272 Mar 21 '17 at 19:02
  • link added to my answer – PhilDW Mar 21 '17 at 19:12