0

How do I connect these two methods to create a simple program where I can drag and drop a program on my desktop or where ever onto my program and it will get the uninstall path and begin the uninstallation process.

So I know how to enable drag and drop

Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.AllowDrop = True
End Sub

Private Sub Form2_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
    For Each path In files

        MsgBox(path)
    Next
End Sub

Private Sub Form2_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

And I also know how to get the uninstallation path for a program

   Dim DestKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

Registry.LocalMachine.OpenSubKey(DestKey).GetSubKeyNames

    UnInstallPath = Registry.LocalMachine.OpenSubKey(DestKey & App & "\").GetValue("UninstallString")

And finally how to uninstall the software

 Dim p As New Process
            p.StartInfo.FileName = "msiexec.exe"
            p.Start()

My question is how do I connect all this to acheive what I want. I cant seem to figure out how I connect the drag and drop to the uninstall process

Bluezap
  • 35
  • 1
  • 10

2 Answers2

0

The key names in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ are GUIDs or strings generated by the installer package used by each product. I don't believe there is any convention to them, so it is unlikely that you'll be able to 'guess' one of them based on having the path to an executable.

Your best bet will (probably) be to retrieve a list of keys, searching through each until you've found the one which contains a InstallLocation matching the path of the executable from your drag and drop, and then use that key's UninstallString.

Use caution, however, because two or more programs can be installed to the same folder. However rare this case might be, you may want to finish searching through the complete list of keys to make sure there are no additional positive matches. If there is only one match, there's a good chance you've found the correct key; if not, you might want to prompt the user to choose the correct key based on each key's DisplayName.

That might look something like:

    'set path from your drag and drop operation
    Dim path As String = "C:\Program Files\Someones Crappy Unwanted Software\"
    Dim DestKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    Dim registryUninstallKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(DestKey)
    If (registryUninstallKey IsNot Nothing) Then
        Dim subKeys As String() = registryUninstallKey.GetSubKeyNames()
        Dim keyMatches As List(Of String) = New List(Of String)
        For Each subKey As String In subKeys
            Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(DestKey & subKey & "\")
            If Not (key Is Nothing) Then
                Dim instLoc As String = Convert.ToString(key.GetValue("InstallLocation"))
                If (instLoc = path) Then
                    keyMatches.Add(subKey)
                End If
            End If
        Next
        For i As Integer = 0 To keyMatches.Count - 1
            'Do something with:
            Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(DestKey & keyMatches(i) & "\")
            If (key IsNot Nothing) Then
                'This will capture the uninstall string for the selected key
                'Dim UnInstallString As String = key.GetValue("UninstallString").ToString
                MessageBox.Show(key.GetValue("UninstallString").ToString)
            End If
        Next
    End If

Edit: Another thing to note is that not every key will have an InstallLocation subkey, so you'll need to find a mix of ways to search through these keys to actually find what you're looking for, in some situations. This example should get you started, though.

Justin Ryan
  • 1,409
  • 1
  • 12
  • 25
  • Hey thank you for that, I have one last question, How do I get the install location from a drag and drop? all I can get now is the path where the icon is located, like C:\Users\example\Desktop\software.ink – Bluezap May 19 '15 at 03:32
  • Here's an example of a way to get a target from a link: http://stackoverflow.com/questions/9454836/vb-net-c-sharp-code-to-access-target-path-of-link-lnk-files-produces-some-wr – Justin Ryan May 19 '15 at 03:43
  • Please look at the solution below, I encounter an error – Bluezap May 19 '15 at 05:14
  • @Bluezap I've adjusted the code above. You must use `OpenSubKey` (which might return `Nothing`) before you can use `GetValue`. – Justin Ryan May 19 '15 at 08:17
  • I tried the code again and it comes up with the same error – Bluezap May 19 '15 at 10:58
  • @Bluezap Updated, again. – Justin Ryan May 19 '15 at 13:16
0

It basically depends on how you get the list of products to uninstall. I would use MsiEnumProductsEx() to get a list of installed (by Windows Installer) products, and then use MsiGetProductInfo() on each of them to get names, versions, main install location etc, and that list includes each of their ProductCode guids, so to uninstall them do MsiConfigureProduct(ProductCode, default, absent). Or call msiexec /x {productcode}. The uninstallstring entry in these Windows Installer products isn't used, so don't trust it. Change it if you like to see if it makes a difference - it doesn't.

I use the appropriate APIs in this case rather than chase the registry because there are real actual APIs to do these things (and interop libraries for managed code). For the non-MSI installs you'll need to look at the registry, but there's a lot of nonsense in there, and you will need the uninstallstring for those, if there is one.

PhilDW
  • 20,260
  • 1
  • 18
  • 28