views:

57

answers:

1

I would like to add an "Uninstall" button to my program that will start the uninstaller to remove the program and then immediately quit. What's the best way to a) determine if there is an uninstaller present, and b) find the correct uninstaller and launch it?

Can I use the registry in a reliable manner? I recall using the registry before to fix some broken installations and was thinking I could iterate over the entries and identify the appropriate one, but I worry that using the registry might not be reliable and/or work on all systems?

Another thought is that I could actually store the uninstall information to the registry during the installation itself, and then use that information to somehow find the correct uninstaller. That seems like it would be the most reliable method, but is there such an identifier that I could store that I could then pass back to Windows Installer?

+2  A: 

Since you taged this with windows-installer, I will assume that you are distributing your application using an MSI. That said, you can call msiexec /x {ProductCode} from your application to initial the uninstall. You can obtain the product code by having the installer write to a registry key/value that your application can read or you can hard code it or your UpgradeCode in your application. If you hard code the UpgradeCode you'll have to call into the Windows Installer API to find out the installed ProductCode for that UpgradeCode.

Christopher Painter
Cool, that sounds just about what I need.
chaiguy
You will want to use the Microsoft.Deployment.WindowsInstaller assembly found in WiX's SDK directory. Take a look at the help file provided for the ProductInstallation::GetRelatedProducts( string UpgradeCode) member. That will return an IEnumeratble<ProductInstallation> which has a getter for ProductCode. If you don't want to include this assembly in your application you'll have to write your own P/Invokes for MSI. MsiEnumRelatedProducts is what you'll need from msi.dll. Take a look at : codeproject.com/KB/cs/msiinterop.aspx Personally I love DTF.
Christopher Painter
My thought was to just store {ProductCode} to a registry key during installation, and then check to see if this key exists inside the program, calling msiexec /x {ProductCode} if it does. Do I really need to access MSI programmatically? Also what's DTF?
chaiguy
I avoided suggesting the registry because it's easier to just ask MSI what the ProductCode is. Also when dealing with the registry in .NET it can get interseting based on bitness.http://blog.deploymentengineering.com/2010/07/net-bitness-pain.htmlDTF is a managed interop for the Windows Installer. Take a look in your start menu under WiX for the DTF.chm.
Christopher Painter