Example of how to use GetRegisteredApplication
Dim strProgramName As String = ""
Dim strShellAppName As String = ""
Dim strTestFileName As String = "C:\windows\CSPU.txt"
If GetRegisteredApplication(strTestFileName, _
Path.GetExtension(strTestFileName), _
strProgramName, strShellAppName) Then
MsgBox("Program Name: " & strProgramName & vbNewLine & _
"Shell Application Name: " & strShellAppName)
Else
MsgBox("Association for " & strTestFileName & " was not found.")
End If
GetRegisteredApplication Function
Public Function GetRegisteredApplication(ByVal ParamFileName As String, _
ByVal FileExtension As String, _
ByRef AppName As String, ByRef ShellAppName As String) As Boolean
Dim StrExt As String
Dim StrProgID As String
Dim StrExe As String
Dim oHKCR As RegistryKey
Dim oProgID As RegistryKey
Dim oOpenCmd As RegistryKey
Dim TempPos As Integer
Try
If FileExtension.StartsWith(".") Then
StrExt = FileExtension
Else
StrExt = "." & FileExtension
End If
Try
oHKCR = Registry.ClassesRoot
oProgID = oHKCR.OpenSubKey(StrExt)
StrProgID = oProgID.GetValue(Nothing).ToString
oProgID.Close()
Catch
Return False
End Try
Try
oOpenCmd = oHKCR.OpenSubKey(StrProgID & "\shell\open\command")
StrExe = oOpenCmd.GetValue(Nothing).ToString
oOpenCmd.Close()
Catch
Return False
End Try
TempPos = StrExe.IndexOf(" %1")
If TempPos > 0 Then
StrExe = StrExe.Substring(0, TempPos)
AppName = StrExe
StrExe = StrExe & " " & Convert.ToChar(34) & _
ParamFileName & Convert.ToChar(34)
ShellAppName = StrExe
Else
AppName = StrExe
ShellAppName = StrExe & " " & Convert.ToChar(34) & _
ParamFileName & Convert.ToChar(34)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Test output
The messagebox contains:
Quote:
Program Name: C:\Windows\system32\NOTEPAD.EXE
Application Name: C:\Windows\system32\NOTEPAD.EXE "C:\windows\CSPU.txt"