Detecting the presence of a directory at install time

狂风中的少年 提交于 2020-01-10 05:00:06

问题


In WiX DirectorySearch can be used to determine if a specific directory exists on the target computer. But I don't understand if there's a consistent way to determine that a directory does not exist.

For example:

<Property Id="INSTALLDIR" Secure="yes">
  <RegistrySearch Id='InstallDir' Type='directory'
    Root='HKLM' Key='Software\Company\Product\Install' Name='InstallPath'/>
</Property>
<Property Id='IS_INSTALLED' Secure='yes'>
  <DirectorySearch Id='IsInstalled' Path='[INSTALLDIR]' />
</Property>

When both the registry key and the directory exist, the IS_INSTALLED property is set to the path returned by DirectorySearch.

When the directory does not exist, IS_INSTALLED appears to be set to "C:\".

Is a condition like:

<Condition>NOT (IS_INSTALLED = "C:\")</Condition>

a reliable way to detect that the directory was found? Is there a better way?

Answer: Here is WiX code based on mrnxs answer that I accepted

<Property Id="PRODUCT_IS_INSTALLED" Secure="yes">
  <RegistrySearch Id='RegistrySearch1' Type='directory'
    Root='HKLM' Key='Software\Company\Product\Version\Install' Name='Path'>
    <DirectorySearch Id='DirectorySearch1' Path='[PRODUCT_IS_INSTALLED]'/>
  </RegistrySearch>
</Property>

<CustomAction Id='SET_INSTALLDIR'
              Property='INSTALLDIR'
              Value='[PRODUCT_IS_INSTALLED]'/>

<InstallExecuteSequence>
  <Custom Action='SET_INSTALLDIR' After='AppSearch'></Custom>
</InstallExecuteSequence>

回答1:


Usually this happens when the property is used as a property-based folder. In this case the CostFinalize action automatically sets the property to a valid path (for example "C:\") so the folder can be used by Windows Installer.

Since this path is generated automatically, you cannot be sure that it will be "C:\" on all your client machines, so you shouldn't use this value in your condition. Instead, you can try this:

  • use a custom property for your folder
  • use a type 51 custom action (property set with formatted text) to set this property to a valid default path (for example "[ProgramFilesFolder]MyCompany\MyProduct")
  • use another property for the search
  • use another type 51 custom action to set the folder property to your search property

For example, if your search is IS_INSTALLED your folder can use IS_INSTALLED_PATH. IS_INSTALLED_PATH can be set to a default path and after AppSearch action you can set it to IS_INSTALLED if the search found something.

This way you can use for conditioning:

IS_INSTALLED

or

NOT IS_INSTALLED



回答2:


Understaing AppSearch's RegLocator and DrLocator patterns can be a little tricky. I reccomend ignoring the condition for a moment and logging the install to verify that AppSearch is properly setting the properties you want. Fix the problems you find on that end first. When it works the property will be set to the value of the registry or the path to the directory.

Then you should be able to use:

<Condition>IS_INSTALLED/>   <!-- it's not important what the value is, just that it exists -->
<Condition>Not IS_INSTALLED/>

Btw, I would avoid using the property INSTALLDIR. In my installers ( InstallShield ) that has special meaning as the main focal point of the installation.




回答3:


Another approach could be this, in this you can continue the Install Sequence if you want to set the InstallDir to anywhere else, if the SystemDir and RegisteryDir is not same

<Property Id="RegisteryDir" Secure="yes">
<RegistrySearch Id='InstallDir' Type='directory'
Root='HKLM' Key='Software\Company\Product\Install' Name='InstallPath'/>
</Property>
<Property Id='SystemDir' Secure='yes'>
<DirectorySearch Id='IsInstalled' Path='[RegisteryDir]' />
</Property>
<CustomAction Id="SET_INSTALL_DIR" Property="INSTALLDIR" Value="[SystemDir] />

<InstallExecuteSequence>
<Custom Action='SET_INSTALLDIR' After='AppSearch'>
SystemDir AND SystemDir=RegisteryDir
</Custom>
</InstallExecuteSequence> 


来源:https://stackoverflow.com/questions/4937674/detecting-the-presence-of-a-directory-at-install-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!