Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 803
Zied.M Avatar asked Dec 24 '22 16:12

Zied.M


2 Answers

You can use Path.GetExtension:

Select Case Path.GetExtension(myDialog.FileName).ToLower()
    Case ".pdf"
        ' ...
End Select
like image 79
Steven Doggart Avatar answered Mar 24 '23 16:03

Steven Doggart


You also could use Extension as blew:

Imports System.IO
Imports System.Runtime.CompilerServices

Module DialogExtensions
    <Extension()>
    Public Function GetFileExtention(ByVal dialog As OpenFileDialog) As String
        Return Path.GetExtension(dialog.FileName)
    End Function
End Module 

And simply use this extension as blow:

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
     Dim s As String = OpenFileDialog1.GetFileExtention()
End If
like image 29
Ali Avatar answered Mar 24 '23 15:03

Ali