Wix: Using KeyPath on Components, Directories, Files, Registry, etc, etc

zam6ak picture zam6ak · Apr 28, 2012 · Viewed 11k times · Source

After reading this answer on "one file per component" approach when using WiX, I was curious to find out what are the best practices when using KeyPath attribute on other elements including Component, Directory, Registry etc, etc.

I am interested in any general suggestion, but here are a couple of concrete questions:

  • If I have an empty directory that installer needs to create should I set KeyPath="yes" on Directory or its parent Component? What if it is not empty?
  • If a File has KeyPath="yes" in a file-per-component scenario, is it necessary or good practice to set it on its parent Component?
  • I read somewhere that instead of setting KeyPath on a File, one should use a Registry key for each File and set KeyPath="yes" on Registry element...Is that really true/necessary?

Thanks!

Edit #1 - Clarification re: Directory

I was aware of Directory not having KeyPath, but was not explicit/detailed in my question. Mainly, I was curious about the usage of KeyPath on a Component when an empty directory has to be created. I am seeing that KeyPath="yes" is in such case being set on the parent Component. But is that enough for the installer to detect/repair missing empty folder? Or should it be used along with registry entry? Example snippet:

<Directory Id="LOGS" Name="Logs">
  <Component Id="LogsDir" Guid="*" KeyPath="yes">
    <CreateFolder Directory="LOGS" />
  </Component>
</Directory>

Answer

Yan Sklyarenko picture Yan Sklyarenko · Apr 28, 2012

In general, you should base your decision on the main idea of KeyPath option. From MSDN:

This value points to a file or folder belonging to the component that the installer uses to detect the component.

So, if you author 1 file per component, you won't face the situation when you accidentally deleted a file and repair didn't bring it back. If you author N files per component, you'll anyway either select one of them to be a KeyPath (and WiX docs encourage you to do this explicitly), or you add an extra registry entry and let it be the KeyPath.

Back to your questions:

If I have an empty directory that installer needs to create should I set KeyPath="yes" on Directory or

Directory element doesn't have a KeyPath attribute.

If a File has KeyPath="yes" in a file-per-component scenario, is it necessary or good practice to set it on its parent Component?

No, basically, this doesn't make sense. If a Component has KeyPath="yes", then the directory this component is installed to becomes a key path. When you set it on a File explicitly, then obviously the file is a key path.

I read somewhere that instead of setting KeyPath on a File, one should use a Registry key for each File and set KeyPath="yes" on Registry element...Is that really true/necessary?

This sounds like nonsense. Again, base on the general need for KeyPath - detect the component. Why do you need an extra registry entry to detect whether a file is there on a file system? It might make sense for N files per component scenario, when you author 1 registry entry per component (that is N files), and let Windows Installer judge by that registry entry, whether the component is considered "not broken".

UPDATE: You don't have to introduce a registry entry just to serve as a key path to help installer tracking an empty folder. It is enough if you add KeyPath='yes' to the parent component.

Don't complicate things. Windows Installer is quite complex as it is. :) Hope this helps.