Removing Files and Directories Windows Installer Does Not Install

Wednesday, July 27, 2005 9:12 AM

The uninstallation behavior of Windows Installer is actually quite efficient.  At uninstall time, Windows Installer will only remove the items that have been installed by the package unless the component containing the resources has been marked as permanent.

At times, this can pose a problem for the setup developer.  Applications will often create files as part of normal everyday usage.  Since these resources did not belong to the original installation, they will remain during an uninstall.  The same applies for registry keys as well.
The functionality is there to avoid such a situation.  Windows Installer has a RemoveFile table which allows you to remove files and folders that were not part of the original installation.  Some people believe that the RemoveRegistry table has the same function as the RemoveFile table.  This is not the case.  Yes, the RemoveRegistry table does remove registry keys, BUT only during the installation of the component that the RemoveRegistry resource belongs too.  In order to remove registry keys that aren't part of the original installation at uninstall time, you will need to use the Registry table.

Populating these tables is quite simple.  The Windows Installer SDK contains detailed information on how to add entries to these tables, but for example I will show you how.

The RemoveFile table has 5 columns, FileKey, Component_, FileName, DirProperty and InstallMode.  Notice the Component column has an underscore after the name.  This is to indicate that the entry in this column is a foreign key in to the Component table.  Here is are some examples of RemoveFile table entries, following will be an explanation of each:

RemoveTempFiles001  |  RemoveFile  |  *  |  FOO  |  2
RemoveAppDir  |  RemoveDir  |  {NULL}  |  INSTALLDIR  |  2

The first example shows how to remove files from directory Foo at uninstall time.  The FileKey entry is arbitrary, you can call it anything you want, but it must be unique.  The Component column must reference an already existing component in the installation.  The FileName column can either be the localizable filename of the file you want to remove, a wildcard, or null.  If it is null, the directory specified in the DirProperty column will be removed if it is empty.  The DirProperty column specifies a directory from the Directory table that contains the file(s) to be removed or the directory to be removed.  The InstallMode column is when the file(s) will be removed.  1 means during install, 2 is during uninstall, 3 means either case.  The second example shows how to remove a directory during uninstall; again, this is assuming that the directory is empty.

I will speak more on removing orphaned registry keys in the next post.

This article is part of the GWB Archives. Original Author: Aaron Shurts

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.