WiX: How to prevent a registry value from being removed on uninstall?

Dirk Vollmar picture Dirk Vollmar · Apr 2, 2009 · Viewed 7.9k times · Source

I want to assert that a certain registry value exists after installation, so I added the following component:

<Component Id="RegistryEntryContextMenuOdt" Guid="4BA5BA24-4F65-4BDF-99EB-CB4B947F31A9" DiskId="1" KeyPath="yes">
    <RegistryKey Id="RegKeyOdt" Root="HKCR" Action="create" Key=".odt">
      <RegistryValue Type="string" Value="openDocument.WriterDocument.1" />
    </RegistryKey>
</Component>

The key/value might already be set before the installation. However, I want that the value is set to my specific value, i.e. that it will be overwritten with my value.

My problem is now that this value is always removed when my product is uninstalled. However, I only want the value to be removed if it was added by my installer. If my installer just modified the value, the previous value should be restored (or, if this is not possible, my value should remain untouched).

Please note that the key itself is not removed on uninstall. This seems to work correctly because I specified Action="create" on the RegistryKey element.

Is there maybe a similar setting for RegistryValue which will create the value but not remove it on install?

UPDATE: Both registry keys under HKCR are machine wide settinge, i.e. they originate from the HKLM\SOFTWARE\Classes branch of the registry.

Answer

Wim Coenen picture Wim Coenen · Apr 2, 2009

You can make sure that your component is only installed when the registry entry does not exist by making use of the NeverOverwrite attribute of the Component element. From the wix documentation for NeverOverwrite:

If this attribute is set to 'yes', the installer does not install or reinstall the component if a key path file or a key path registry entry for the component already exists.

You may also need to set the KeyPath attribute on the Registry element to yes to make it unambiguous that the registry entry is the component key path.

If you do want to set the registry value even if it already exists, but you don't want to remove it on uninstall, then you can use the Permanent attribute of the Component element instead.