24

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>
Community
  • 1
  • 1
zam6ak
  • 7,229
  • 11
  • 46
  • 84

1 Answers1

26

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.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
  • 11
    There is one case where using an extra registry entry as the key path is recommended: when you have a component which doesn't contain any other resource that can serve as a keypath. The typical example is a component which installs a shortcut. – Wim Coenen Apr 28 '12 at 08:45
  • @Yan Sklyarenko Thanks for the detailed answer. I was aware that Directory has no KeyPath, but I updated my question to clarify the context in which was related to KeyPath. BTW, you are correct when saying that Win Installer is complex, and my goal was to make sure I don't complicate my installs. That is why I asked the question.:) The tutorial on WiX is quite good, but I was unable to find a source of "best practices" that would include useful tips (such as one Wim Coenen posted in his comment) – zam6ak Apr 30 '12 at 13:22
  • @zam6ak, I've updated my answer in terms of keypath and empty folder. – Yan Sklyarenko May 03 '12 at 07:12
  • Another use of an extra registry entry is when you don't want to re-create a file on repair if it gets deleted. Useful for files in "user" owned directories to make it so they can delete files they don't want. – Mitch Aug 14 '16 at 21:09