1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Frequently Asked Questions

Determining the description of a file’s file type

Determining the description of a file's file type

Determining the description of a file's file type

The Windows Explorer displays a short description of a file's file type when
selecting it.  This information can be obtained by a call to
'SHGetFileInfo'.  The structure 'SHFILEINFO' needs to have byte-aligned
members, so we need to specify 'Pack:=1' in its attributes.
'GetFileDescription' encapsulates the p/invoke call into an easy-to-use

Imports System.Runtime.InteropServices
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" ( _
    ByVal pszPath As String, _
    ByVal dwFileAttributes As Int32, _
    ByRef psfi As SHFILEINFO, _
    ByVal cbFileInfo As Int32, _
    ByVal uFlags As Int32 _
) As IntPtr

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=1)> _
Private Structure SHFILEINFO
    Private Const MAX_PATH As Int32 = 260
    Public hIcon As IntPtr
    Public iIcon As Int32
    Public dwAttributes As Int32
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> _
    Public szDisplayName As String
    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    Public szTypeName As String
End Structure

Private Const SHGFI_TYPENAME As Int32 = &H400

Private Function GetFileDescription(ByVal FileName As String) As String
    Dim shfi As SHFILEINFO
    SHGetFileInfo(FileName, 0, shfi, Marshal.SizeOf(shfi), SHGFI_TYPENAME)
    Return shfi.szTypeName
End Function