none
OpenFileDialog >>> File Type Selection

    Question

  • Hello All,

    I am using OpenFileDialog to brows and select for file (working fine). 

    Is it possible to have only a specific file type as pre-selection? Like *.txt or *.png etc?

    Tuesday, April 25, 2017 10:11 AM

Answers

  • Hi -OSD-,

    I put one button and OpenFileDialog control in the Form, please refer to the code below.

    Imports System.IO
    
    Public Class OpenFileDialog
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myStream As Stream = Nothing
            OpenFileDialog1.InitialDirectory = "D:\"
            OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try
                    myStream = openFileDialog1.OpenFile()
                    If (myStream IsNot Nothing) Then
                        ' Insert code to read the stream here.
                    End If
                Catch Ex As Exception
                    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                Finally
                    ' Check this again, since we need to make sure we didn't throw an exception on open.
                    If (myStream IsNot Nothing) Then
                        myStream.Close()
                    End If
                End Try
            End If
    
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 27, 2017 3:18 AM
    Moderator
  • Hello All,

    I am using OpenFileDialog to brows and select for file (working fine). 

    Is it possible to have only a specific file type as pre-selection? Like *.txt or *.png etc?

    As Cherry has shown, you can use the .Filter property for this:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filter(v=vs.110).aspx

    Also, in conjunction with that, use the .FilterIndex property to select which filter is the default:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filterindex(v=vs.110).aspx

    Note that the .FilterIndex is one-based, not zero-based.

    ***** EDIT *****

    An example:

    Private Sub SomeMethod() Using ofd As New OpenFileDialog With ofd .CheckFileExists = True .CheckPathExists = True .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" .FilterIndex = 1 .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) .Title = "Select The Text File To Open" End With If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then ' ofd.FileName has the full path of the ' file that the user chose. End If End Using End Sub



    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thursday, April 27, 2017 7:09 AM

All replies

  • Hi -OSD-,

    I put one button and OpenFileDialog control in the Form, please refer to the code below.

    Imports System.IO
    
    Public Class OpenFileDialog
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim myStream As Stream = Nothing
            OpenFileDialog1.InitialDirectory = "D:\"
            OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try
                    myStream = openFileDialog1.OpenFile()
                    If (myStream IsNot Nothing) Then
                        ' Insert code to read the stream here.
                    End If
                Catch Ex As Exception
                    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                Finally
                    ' Check this again, since we need to make sure we didn't throw an exception on open.
                    If (myStream IsNot Nothing) Then
                        myStream.Close()
                    End If
                End Try
            End If
    
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 27, 2017 3:18 AM
    Moderator
  • Hello All,

    I am using OpenFileDialog to brows and select for file (working fine). 

    Is it possible to have only a specific file type as pre-selection? Like *.txt or *.png etc?

    As Cherry has shown, you can use the .Filter property for this:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filter(v=vs.110).aspx

    Also, in conjunction with that, use the .FilterIndex property to select which filter is the default:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filterindex(v=vs.110).aspx

    Note that the .FilterIndex is one-based, not zero-based.

    ***** EDIT *****

    An example:

    Private Sub SomeMethod() Using ofd As New OpenFileDialog With ofd .CheckFileExists = True .CheckPathExists = True .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" .FilterIndex = 1 .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) .Title = "Select The Text File To Open" End With If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then ' ofd.FileName has the full path of the ' file that the user chose. End If End Using End Sub



    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thursday, April 27, 2017 7:09 AM
  • Thanks guys, it worked :)
    Thursday, April 27, 2017 11:04 AM
  • Thanks guys, it worked :)

    I'm glad it helped. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, April 27, 2017 12:10 PM