how to get the install path for a program using vb.net

Cynolycus 260 Reputation points
2023-10-22T06:57:01.93+00:00

I'm trying to get the install path of a program from the registry using code that I will include in my project.

I pieced together some code to do the job but found it is looking in the wrong place.

The registry entry that I need to get is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

I have manually checked this so I know it is there but I can't seem to get this code to work.

My problem is that even though the code is set to use "Software\Microsoft\Windows\CurrentVersion\Uninstall" it is looking in HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall

The test form only has one button called Button1 and one ListBox called ListBox1

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim List As New List(Of String)
      Dim Regkey, Subkey As Microsoft.Win32.RegistryKey
      Dim ProgramName As String
      Dim Regpath As String = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
      Regkey = My.Computer.Registry.LocalMachine.OpenSubKey(Regpath)
      Dim Subkeys() As String = Regkey.GetSubKeyNames
      For Each subk As String In Subkeys
          Subkey = Regkey.OpenSubKey(subk)
          ProgramName = Subkey.GetValue("DisplayName", "").ToString
          'If ProgramName <> "" Then
          List.Add(Regkey.ToString & Chr(9) & subk & Chr(9) & ProgramName)
          'End If
      Next
      List.Sort()
      For i = 0 To List.Count - 1
          ListBox1.Items.Add(List(i))
      Next
  End Sub

Any ideas why it seems to revert to "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" instead of using "Software\Microsoft\Windows\CurrentVersion\Uninstall" and how to fix it?

Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-10-22T09:26:38.2666667+00:00

    It seems that your program runs as a 32-bit application in 64-bit Windows and is affected by “Registry Redirector”:

    The behavior depends on compiler options like “Target CPU”, “Prefer 32-bit”.

    To read the 64-bit data even in case of 32-bit application, try this code:

    Dim lm As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    Regkey = lm.OpenSubKey(Regpath)
    Dim Subkeys() As String = Regkey.GetSubKeyNames
    For Each subk As String In Subkeys
    . . .
    

    Or maybe you should create a new compiler configuration, specifying the 64-bit platform instead of Any CPU or 32-bit.

    To get just the path, see also the value of Application.StartupPath.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.