Reputation: 115
I am upgrading my custom windows service using MSI installer. I am using C# code to start MSI process to first uninstall the service and then install new version.
I need to make sure that before MSI starts installing new version ,previous version is uninstalled. How do I add this check in c#?
Upvotes: 1
Views: 1010
Reputation:
I recommend using WIX to do this. A wrapper around MSI http://wixtoolset.org/documentation/
Make sure to increment the version as a best practice.
Key is to increment the Version attribute or set AllowSameVersionUpgrades="yes" and not change UpgradeCode="[your unique upgradecode here]". Make sure that the UpgradeCode attribute remains the same it has to be static so should not be set to * which will generate a random GUID.
<MajorUpgrade AllowSameVersionUpgrades="yes" DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<Product
Name="Sample"
Id="*"
UpgradeCode="4c79fec3-a6b7-46eb-90d6-46688a7f1662"
Manufacturer="Sample"
Version="0.1.3.0"
Language="1033"> ... />
Your question is how to do this in C#
check the registry key HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\
SC QUERY you probably just want to replace that part with C# code. I strongly advise against using most of the built-in Windows command-line programs.
Upvotes: 2
Reputation: 115
I am using InstallSheild.. This issue was happening because by the time MSI installer could remove the SERVICE from registry, my code starts installing the new version of it.
So the MSI does remove the service from machine registry but because of the time lag in this process, new version installation fails. This happens in server machines mostly. Don't know the reason for it.
To fix it, i am now checking service existence before installation using sc query servicename
and so while service exists I am doing thread.sleep(1000)
Please share a better approach if any. But I have to do it using InstallSheild only.
Upvotes: 0