2

Is there a way to get the file type displayed in windows explorer in VB.net

i.e in windows explorer in details view one can see for example

Name          Date Modified           Type                           Size
A.PDF         05/06/2017 5:54PM       Adobe Acrobat reader           150kb
B.DOCX        05/06/2017 5:00PM       Microsoft Word Document        100kb
etc.

I want to get the type. I cant seem to find a way to get there. It feels like this should be very easy.

Dim infoReader As System.IO.FileInfo
infoReader = My.Computer.FileSystem.GetFileInfo(txtFileName.Text)

FileInfo gets me modified data and size of the file.. but not the type.

Appreciate forums help!

. I.e. for .pdf file the display with would "adobe acrobat document" .xls file would be "Microsoft excel worksheet"

James Thorpe
  • 28,613
  • 5
  • 64
  • 82
mattrak
  • 21
  • 1
  • 2
  • 2
    [C# version](https://stackoverflow.com/questions/3780028/how-can-i-get-the-description-of-a-file-extension-in-net) of this question - you may be able to translate the C#. – James Thorpe Jun 05 '17 at 14:22
  • Current NET versions include a [MimeMapping Class](https://msdn.microsoft.com/en-us/library/system.web.mimemapping.aspx) or you could build a dictionary of common/expected mime types and look up the extension. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jun 05 '17 at 14:24
  • Possible duplicate of [How can I get the description of a file extension in .NET](https://stackoverflow.com/questions/3780028/how-can-i-get-the-description-of-a-file-extension-in-net) – Knowledge Cube Jun 05 '17 at 15:25

2 Answers2

4

You can use below VB.net code for getting the file type description. Basically you have to use SHGetFileInfo API to get that information.

Imports System.Runtime.InteropServices

Module Get_File_Type

Sub Main()
    Dim info As New NativeMethods.SHFILEINFO()

    Dim fileName As String = "C:\TEST\TEST.xlsx"
    Dim dwFileAttributes As UInteger = NativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL
    Dim uFlags As UInteger = CUInt(NativeMethods.SHGFI.SHGFI_TYPENAME Or NativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES)

    NativeMethods.SHGetFileInfo(fileName, dwFileAttributes, info, CUInt(Marshal.SizeOf(info)), uFlags)

    Console.WriteLine(info.szTypeName)
    Console.ReadLine()
End Sub

End Module

NotInheritable Class NativeMethods
    Private Sub New()
    End Sub
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As UInteger
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure

    Public NotInheritable Class FILE_ATTRIBUTE
        Private Sub New()
        End Sub
        Public Const FILE_ATTRIBUTE_NORMAL As UInteger = &H80
    End Class

    Public NotInheritable Class SHGFI
        Private Sub New()
        End Sub
        Public Const SHGFI_TYPENAME As UInteger = &H400
        Public Const SHGFI_USEFILEATTRIBUTES As UInteger = &H10
    End Class

    <DllImport("shell32.dll")> _
    Public Shared Function SHGetFileInfo(pszPath As String, dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, cbSizeFileInfo As UInteger, uFlags As UInteger) As IntPtr
    End Function
End Class
Bob R
  • 41
  • 7
  • Thank you Bob, it works great! How do you come up with such code... Wow! – mattrak Jun 05 '17 at 16:48
  • Your welcome mattrak, I am using similar code for getting file related information. – Bob R Jun 05 '17 at 21:49
  • There's no need for all of this gibberish... There are other methods of doing such ... Also `Basically you have to use SHGetFileInfo API to get that information`, I strongly disagree with that statement. Do you ***not know of another way***? – zaggler Jun 06 '17 at 00:49
  • @mattrak see [***here***](https://github.com/Raymai97/BadAPPLEanim/blob/master/SOURCE%20v2.0/baShared.vb), that's not the only place that code live's either; there are other places as well that have this; it's not new. – zaggler Jun 06 '17 at 01:38
1

Honestly this is all you would need...

Public Shared Function GetFileType(ByVal Extention As String) As String
   Return My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\" & My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\" & Extention, "", Extention).ToString, "", Extention).ToString
End Function

To use it just get the extension of your file and then pass that into the function. Below will get your extension you would need.

IO.Path.GetExtension(YOUR FILE PATH HERE)

So all wrapped up...

 Dim strName As String = GetFileType(IO.Path.GetExtension(YOUR FILE PATH HERE))
zaggler
  • 7,254
  • 6
  • 26
  • 47
  • Codexer, thank you Buddy! It is amazing the different ways to skin the cat. Your solution was lot simpler and infact I can understand what you are doing. Thank you once again – mattrak Jun 10 '17 at 07:53