As a follow-up to my last post, I'd like to discuss how a program should utilize the Windows registry as opposed to how it's often used. Read on for the goods.
The Windows registry (herein referred to simply as 'the registry') is the source of a lot of my end-user grief with Windows. Here's the core problem-- programs depend too much on it.
Here's an easy test to see if your favorite application will allow itself to be moved easily or not: rename the directory it lives in and then try to run it (change it back after you're done playing). If it doesn't load properly or operate normally, then it's probably relying on some registry setting to tell it where it lives on your computer. This is rather silly, since one of the basic tasks a process can do is ask the operating system where it's running from. You don't have to store that information statically in the first place.
Another test is to delete the registry keys for the application entirely (if you're not sure what I'm talking about or how to do this, then you shouldn't even attempt it until you know more about how the registry works). I would suggest you back up the settings before you delete them and restore them after your expirment is complete. Again, if the program fails to load and operate normally, it relies too much on the registry.
So what SHOULD these applications do to survive file system changes or loss of their registry settings? Here are some tips:
- Always, always, always have backup settings in case the registry data isn't there. Don't assume that the installation program created your default registry settings for you.
- Don't store the file path to where the application is installed. Determine it dynamically. If the .exe file is running from C:\Program Files\My App\app.exe, then the installation directory would be C:\Program Files\My App.
- If your application determines that registry settings are missing, rebuild them. Handle missing or bad data gracefully, rather than showing an error dialog and terminating the application.
In short, don't trust the registry. Caching user and application preferences in there is fine, but if they don't exist, revert to defaults instead of crashing.
A good example of what I'm talking about is WinZip. I've copied a WinZip installation from one PC to another (where the target PC had never had WinZip installed on it before) without any problems whatsoever. Running WinZip on the new PC even caused it to go through it's configuration dialog where it asks for initial user preferences and makes the proper file associations and shell enhancements for operation. Now that's more like it.