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
.
You can use Path.GetExtension
:
Select Case Path.GetExtension(myDialog.FileName).ToLower()
Case ".pdf"
' ...
End Select
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With