0

We have an Excel AddIn say A written in C#, Add-In Express. The installer is built from setup project in VS. Now we want to integrate it to another bigger add-in application say B. we want to be able to uninstall A during installation of B. B is also written in C#, but its installer is built from Advanced Installer.

I tried VBA like below, AddIns only contain "A XLL Add In" but not "A COM Add In". so it does not work. So I am thinking to write an exe to detect if A is installed and if so, uninstall it. and call the exe in installer of B.
Anyone know how to "uninstall a program in C#" ? or there is better solutions? thanks Once I can detect A and uninstall it in an exe, I will be able to hook it in installer of B. Installer/Uninstaller class in .NET is not an option since I am not using them in installer of B.

Dim item As AddIn
Set item = Application.AddIns("A COM Add In")

If Not item Is Nothing Then
    item.Installed = False
    'item = Nothing 'Not sure if this does anything
End If

    Dim item As AddIn
Set item = Application.AddIns("A XLL Add In")

If Not item Is Nothing Then
    item.Installed = False
    'item = Nothing 'Not sure if this does anything
End If
Cœur
  • 37,241
  • 25
  • 195
  • 267
toosensitive
  • 2,335
  • 7
  • 46
  • 88

4 Answers4

2

You can uninstall the previous version by invoking MSI directly:

msiexec /x YOUR-PRODUCT-CODE

Replace YOUR-PRODUCT-CODE with the real product ID used in your MSI package which installed the AddIn A.

You may want to add /qn option to completely suppress its UI.

MSI also provides API you can use to find out if the product is installed and to uninstall it.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
1

You can use the same Upgrade Code for both installers. If Upgrade Code is the same and installer B has a higher version than A, then A package will be uninstalled by MSI during the installation of B.

ogggre
  • 2,204
  • 1
  • 23
  • 19
0

Your approach is correct - in particular the exe you are creating should be Setup.exe which will ensure that the pre-requisites are met. Ie it will uninstall A.

How you create setup.exe is very dependent on what tools you have to hand though - sorry I can help you much there as I use wix3.0 which is quite limited in this particular regard.

dice
  • 2,820
  • 1
  • 23
  • 34
  • Thanks, Sorry did not make myself clear. I already created installer B and it works fine. My question is how to uninstall a program from C#? Once it is solved, I will be able to hook the exe in installer of B. – toosensitive Feb 27 '12 at 21:10
  • 1
    ok - so long as you dont try running it while another msi is already executing I think your best bet is to Process.Start() a call to msiexec.exe – dice Feb 27 '12 at 21:16
  • thanks @dice. Yes, I found sample code to uninstall using msiexec.exe @ http://social.msdn.microsoft.com/forums/en-us/netfxbcl/thread/CA866421-1D38-459E-822F-622E57046A04. It works. thanks! – toosensitive Feb 27 '12 at 22:16
0

You can use WMI - ManagementObject to accomplish this.

 ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM    Win32_Product"); 

 ManagementObject product = search.Get().Cast<ManagementObject>().ToList().Where(obj
=> obj["Name"].ToString().StartsWith([Name of product to Uninstall])).FirstOrDefault();

 if (uninstallObject != null)
    object result = product.InvokeMethod("Uninstall", null);
Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
  • Win32_Product is very slow and buggy. I'd suggest using the Microsoft.Deployment.WindowsInstaller interop found in WiX's DTF library. It has managed classes that directly encapsulate the underlying MSI Win32 API calls that Win32_Product does such a poor job of implementing. – Christopher Painter Feb 28 '12 at 14:58
  • @ChristopherPainter any suggestions on where to look for examples of using the `Microsoft.Deployment.WindowsInstaller` interop? – Kyle Feb 22 '13 at 17:43
  • 1
    My blog has many examples. The library comes with an SDK Help File that is very thorough and includes samples. – Christopher Painter Feb 22 '13 at 21:25