Search code examples
visual-studiowindows-installersetup-project

How to create a MSI property based on whether a file exists at a given path?


One of the conditions for showing some UI controls in my Setup application is based on whether a file exists.

This check can't be done through custom actions since even the OnBeforeInstall event happens after install, and the dialog I want to alter is shown before that.

If I was using WiX it would be simple as

<Property Id="FILEEXISTS">
   <DirectorySearch Id="CheckFileDir"
                 Path="[CommonAppDataFolder]Manufacturer\Product"
                 Depth="0">
      <FileSearch Id="CheckFile"
              Name="Filename.ext" />
   </DirectorySearch>
</Property>

How to do it without WiX?


Solution

  • You'll need a post build JScript for your MSI file. Or you can do it manually in Orca.

    var installer = WScript.CreateObject("WindowsInstaller.Installer");
    var filespec = WScript.Arguments(0);
    var msiOpenDatabaseModeTransact = 1;
    var database = installer.OpenDatabase(filespec, msiOpenDatabaseModeTransact);
    
    Execute("INSERT INTO `AppSearch` (`Property`, `Signature_`) VALUES ('FILEEXISTS', 'CheckFile')");
    Execute("INSERT INTO `DrLocator` (`Signature_`, `Parent`) VALUES ('CheckFile', 'CheckFileDir')");
    Execute("INSERT INTO `DrLocator` (`Signature_`, `Path`, `Depth`) VALUES ('CheckFileDir', '[CommonAppDataFolder]Manufacturer\\Product', 0)");
    Execute("INSERT INTO `Signature` (`Signature`, `FileName`) VALUES ('CheckFile', 'Filename.ext')");
    
    function Execute(sql) {
        view = database.OpenView(sql);
        view.Execute();
        view.Close();
    }