Search code examples
vb.netopenfiledialogrecover

Get the file extension of the file chosen by OpenFileDialog


I am using an OpenFileDialog to allow the user to select a file. How do I then get the extension of the file that they chose? I need to perform a different action depending on the type of file. For instance, if they choose a PDF file, I need to launch a PDF viewer, but if it's am image, I need to show it in a PictureBox.


Solution

  • You can use Path.GetExtension:

    Select Case Path.GetExtension(myDialog.FileName).ToLower()
        Case ".pdf"
            ' ...
    End Select