Monday, November 17, 2008

Avoiding custom actions by using the -File tables


It has often been remarked that one should avoid using custom actions if a built-in table and action will do. I’ll repeat the remark in the hope that it will help: Don’t use a custom action if a built-in table and action will do.

For example, my heart sinks every time I see someone using VBScript custom actions and the FileSystemObject to manipulate files, when three tables will handle many common cases: the RemoveFile table, the DuplicateFile table, and the MoveFile table. Taking each one in turn:

The RemoveFile table is where you specify files that you want to remove during uninstallation (most common case), installation, or both. (In each of these tables, and in fact most tables related to data transfer, the action is tied to a component. In this case, RemoveFile records are associated with a component, and the file is removed when the associated component is installed or uninstalled.) The removal occurs when the RemoveFiles action runs, which must be before InstallFiles runs.

For example, suppose you know that on first launch, your product will create a file called file_to_remove.ext in the main program directory, represented by the directory property INSTALLDIR. (The main directory property name might be different depending on the development environment or help file you’re using.) Windows Installer will normally err on the side of caution during uninstallation and leave behind any files it didn’t install, but to remove the file at uninstallation time, you can add a record similar to the following to the RemoveFile table.

Field nameSample valueRemarks
FileKeyRemoveMyFilearbitrary identifier for this record
Component_main_exepick a component
FileNamefile_to_remove.extcan use wildcards; leave blank to remove empty directory; won’t delete subdirectories
DirPropertyINSTALLDIRmust be property with no brackets; can’t be raw path
InstallMode2that is, when component “main_exe” is removed

Having set this up, when you uninstall your product, file_to_remove.ext will be removed instead of being left behind.

For some reason, the RemoveRegistry table removes extra registry information during installation but not uninstallation. To remove extra registry information during uninstallation, look into the hyphen “-” flag for the Registry table.

The DuplicateFile table is where you specify files that you want to copy, assuming you also installed the files with the File table. The following example copies file_to_copy.ext to INSTALLDIR with the new name newname.ext, assuming the main_exe component has been selected to be installed locally.

Field nameSample valueRemarks
FileKeyDuplicateMyFilearbitrary identifier
Component_main_exepick one
File_file_to_copy.extidentifier from File table
DestNamenewname.extnew name if you want one, blank if you don’t
DestFolderINSTALLDIRproperty with no brackets

These files are processed by the DuplicateFiles action, which must occur after InstallFiles.

The MoveFile table is similar to the DuplicateFile table, but the files that you want to move or copy need not have been installed with the File table. The files are moved when the MoveFiles action runs (before InstallFiles), and will not be removed when the product is removed.

No comments: