如何检测软件是否已安装及其安装路径



我需要检查,用我的VB.net软件,如果一个程序已经安装在我的软件执行之前。我环顾四周,发现我可以通过检查Windows中的注册表编辑器来实现这一点。但是如何检测该软件的安装路径呢?我的意思是,在我的例子中,软件安装在D盘上,而不是C盘上,所以,如果我映射C路径,我会收到一个错误。我怎样才能得到它的确切安装路径?希望我说得越清楚越好,谢谢大家都会回答我的。

问好

这里有两个如何检查一些东西的例子。

首先,这是我的一个工具检查自己已经安装的版本的方式。这不是你想要的,但是别担心——我马上就会讲到:

Public Function GetSolutionVersion(MyApp As String)
Dim uninstallKey As String = "SOFTWAREMicrosoftWindowsCurrentVersionUninstall"
Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(uninstallKey)
For Each skName In rk.GetSubKeyNames
Dim name As String = Registry.LocalMachine.OpenSubKey(uninstallKey).OpenSubKey(skName).GetValue("DisplayName")
If Not name Is Nothing Then
If name.ToString.ToLower = MyApp.ToLower Then
Try
Dim displayversion As String = Registry.LocalMachine.OpenSubKey(uninstallKey).OpenSubKey(skName).GetValue("DisplayVersion")
Return displayversion
Catch ex As Exception
Return ""
End Try
End If
End If
Next
End Using
Return ""
End Function

如果你真的浏览了卸载中的一些条目,你会看到其中一些——但绝对不是全部——有一个InstallLocation键,告诉你在哪里找到这个程序。这取决于特定的安装程序。您也可以通过查看UninstallPathUninstallString键来找出路径;同样,这取决于安装程序。

作为第二个例子,假设我们想看看是否安装了Firefox -以及在哪里:

Private Sub CheckFirefox()
'Dim readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USERSoftwareMyApp", "Name", Nothing)
'ComputerHKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Pathsfirefox.exe

Dim HasFirefox as Boolean = False
Try
Dim regvalue = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Pathsfirefox.exe", "", Nothing)
If regvalue <> Nothing Then
'Found firefox!
FirefoxFullPath = regvalue
HasFirefox = File.Exists(FirefoxFullPath)
If HasFirefox = False Then
MsgBox($"WARNING!  Registry says Firefox is at ({regvalue}), but no such file was found")
Else
MsgBox($"Firefox found at {FirefoxFullPath}")
End If
Else
MsgBox("Firefox path not found in registry")
HasFirefox = False
End If

If HasFirefox = True Then

'Do something...

End If
Catch ex As Exception        
MsgBox($"Unable to find Firefox in registry because an error occurred: {ex.ToString}",, "Error")
HasFirefox = False
End Try
End Sub

知道在注册表中正确的位置以及如何获得正确的键值是最难的部分-希望这有助于。然而,并不是每个软件都将在App Paths中列出——这取决于它们是否经过了注册过程。

最后,如果这些都不起作用,考虑一下用户将如何启动这个特定的程序。如果他们要使用桌面快捷方式或菜单项,你可以扫描这些并检索其属性。

最新更新