Differentiating between uninstalling/installing and upgrading an application
So, I have this application for which we are producing further development versions.
If the application gets uninstalled by the user (from Add/Remove Programs let's say), then I want the application to delete a certain folder in the file system which contains app-related information.
However, if the application is upgraded (by downloading a newer installer for this app and installing it), then I want it to keep that folder.
I am under the impression (at the moment) that it is not possible to differentiate between uninstalling/installing and just updating an application from one version to another, because the .NET deployment projects tr开发者_JS百科eat upgrading as just another uninstall/install procedure, but this should be possible somehow.
Thanks for taking the time and whatever answers I may get.
The installer has three GUID codes, ProductCode, PackageCode and UpgradeCode that you can use to manage these scenarios: The UpgradeCode is like an application id and shouldn't change between versions; the PackageCode identifies a release of your setup and the ProductCode identifies a release of your software. You can change the ProductCode by incrementing the ProductVersion value.
Windows uses the ProductCode to see if the application is already installed on a system. If you reinstall using an installer with the same ProductCode and PackageCode you get a Repair/Uninstall option.
This is not a direct answer, but have you considered using third-party installation software? We use InnoSetup for distributing a .Net application, and we find it fairly easy to use and very flexible. It could certainly handle what you are describing.
Have you tried using the ClickOnce deployment? It handles all the install and upgrading of the application. By changing the publish status of the files included in the update you can control if the file gets overwritten with an update or stays the same. When the application is uninstalled all the files will be removed.
Using a wix installer will allow you to make this differentiation. This is a more hands on approach, but it will work.
You could also try adding some custom preinstall action, which copies the mentioned folder to some temporary folder and then a post install action copies it back to its place. But please, first consider the above sugestions, such as using a more powerfull installer, or clickOnce.
I've eventually also figured out a way to check if the application is upgrading or if it is a fresh install:
- The application leaves some information about itself in the registry (application name, version number etc.). This is usually stored within
LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{Application's GUID}
When the application is uninstalled that information is removed.
- But you can check for that information when you are installing a version of your application (this can be done by adding some custom actions in the deployment project). I check it under the BeforeInstall method and look in the registry for the DisplayVersion value, like so:
RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{AppGUIDHere}");
string version = (string)regKey.GetValue("DisplayVersion");
This value will contain the version that is currently installed on your computer. If this installed version is smaller than the version of the application you are trying to install, then you follow the logic path of UPGRADING
If you can't find anything in the registry under that key, that means that this is a fresh install (there is no other application before this one installed on the computer). And here you follow the other logic path of just installing
Hope this helps somebody.
精彩评论