Click here to Skip to main content
15,887,135 members
Articles / Programming Languages / Visual Basic
Tip/Trick

How to get EXE associated with a File Extension in the Registry

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 Sep 2013CPOL 21.3K   4   3
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

VB
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
VB
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
I’m retired. When I started my career, programming projects consisted of plugging wires into plug boards to create punch card processing applications to be run on electrical accounting machine like the IBM 402, 407, 085, 088, 514, 519, etc. From there, I moved to writing SPS and Autocoder applications on an IBM 1401 with 4K of memory eventually upgraded to 16K of memory. After many years of migrating my skills to various languages on various hardware platforms, I became an Information Technology Director where I didn’t need to program anymore. So, starting in 1996, I volunteered my time with a local community cable television organization and built some applications to help them run their operations. Originally in Clipper Summer 1987 and later Clipper 5.2, I migrated and enhanced those applications to VB .NET 2003 in 2003. I retired from my full-time job in 2010. Since then, I have continued to support the local community cable tv organization's applications. In 2013, I migrated the VB .NET 2003 Solution to VB .NET 2012 so that it can run on 64-bit computers and interact with Microsoft Office 2010. The upgrade went smoothly. In mid 2013, I developed a VB .NET 2012 application for them to download election results data from the Secretary of State's web site, format the results and send them to a VizRT character generator for on-air display.

Comments and Discussions

 
QuestionWhy Pathname? Pin
Ian Brooke1-Jul-23 8:37
Ian Brooke1-Jul-23 8:37 
SuggestionRe: Why Pathname? Pin
Mike Meinz2-Jul-23 3:00
Mike Meinz2-Jul-23 3:00 
Generalthanks Pin
Ivan Ferrer27-Feb-15 4:09
Ivan Ferrer27-Feb-15 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.