Visual Basic.NET 7.x (2002/03)Windows 2008Visual Basic 9 (2008)Visual Basic 8 (2005)Windows VistaWindows 2003Windows 2000Windows XPIntermediateDevWindows.NETVisual Basic
How to get EXE associated with a File Extension in the Registry





5.00/5 (1 vote)
Function returns exe name and command line for the provided filename and extension.
Introduction
This function returns the EXE filename and the command line for a submitted filename and filename extension. It uses the HKEY_CLASSES_ROOT registry hive to find the "shell open" program associated with a filename extension.
Using the code
Copy the GetRegisteredApplication
function to your program.
Use the example below to show you how to call GetRegisteredApplication
to retrieve
the EXE associated with the filename extension and the shell commandline.
GetRegistryApplication Function
Imports Microsoft.Win32
Imports System.IO
...
Public Function GetRegisteredApplication( _
ByVal ParamFileName As String, _
ByVal FileExtension As String, _
ByRef AppName As String, _
ByRef ShellAppName As String) As Boolean
'
' Return registered application based on the filename extension
'
Dim strExtension As String
Dim strProgramName As String
Dim strEXEFilename As String
Dim regkey_HKEY_CLASSES_ROOT As RegistryKey ' HKEY_CLASSES_ROOT
Dim regkey_ProgID As RegistryKey
Dim regkey_OpenCommand As RegistryKey
Dim intIndex As Integer
Try
' Add starting dot to extension
If FileExtension.StartsWith(".") Then
strExtension = FileExtension
Else
strExtension = "." & FileExtension
End If
' Get Programmatic Identifier for this extension
Try
regkey_HKEY_CLASSES_ROOT = Registry.ClassesRoot
regkey_ProgID = regkey_HKEY_CLASSES_ROOT.OpenSubKey(strExtension)
strProgramName = regkey_ProgID.GetValue(Nothing).ToString
regkey_ProgID.Close()
Catch
' Nothing found
Return False
End Try
' Get application associated with the file extension
Try
regkey_OpenCommand = _
regkey_HKEY_CLASSES_ROOT.OpenSubKey(strProgramName & "\shell\open\command")
strEXEFilename = regkey_OpenCommand.GetValue(Nothing).ToString
regkey_OpenCommand.Close()
Catch
' No default application is registered
Return False
End Try
intIndex = strEXEFilename.IndexOf(" %1")
If intIndex > 0 Then
' Replace %1 placeholder with ParamFileName
strEXEFilename = strEXEFilename.Substring(0, intIndex)
AppName = strEXEFilename
strEXEFilename = strEXEFilename & " " & _
Convert.ToChar(34) & ParamFileName & Convert.ToChar(34)
ShellAppName = strEXEFilename
Else
' No %1 placeholder found, append ParamFileName
AppName = strEXEFilename
ShellAppName = strEXEFilename & " " & _
Convert.ToChar(34) & ParamFileName & Convert.ToChar(34)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Example
Dim strProgramName As String = ""
Dim strShellCommandLine As String = ""
Dim strTestFileName As String = "C:\windows\CSPU.txt"
If GetRegisteredApplication(strTestFileName, Path.GetExtension(strTestFileName), _
strProgramName, strShellCommandLine) Then
Console.WriteLine("Program Name: " & strProgramName & vbNewLine & _
"Shell CommandLine: " & strShellCommandLine)
Else
MsgBox("Association for " & strTestFileName & _
" was not found.")
End If
Example Results
Program Name: C:\Windows\system32\NOTEPAD.EXE
Shell CommandLine: C:\Windows\system32\NOTEPAD.EXE "C:\windows\CSPU.txt"
History
Version 1: 5 September 2013.