VB.NET - To filter folder with extension - Asked By abinav shankar on 05-Sep-11 08:51 AM

Hi
 
When i select a folder the folder should display all the files with extensions what i want for example if i want files with extensions .xml when i select the folder i want the folder to list on the xml files inside the folder how to do it
dipa ahuja replied to abinav shankar on 05-Sep-11 09:02 AM

Private Sub button2_Click(sender As Object, e As EventArgs)
  fl = New OpenFileDialog()
  fl.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"

  If fl.ShowDialog() = DialogResult.OK Then
    textBox3.Text = fl.FileName
  End If
End Sub

smr replied to abinav shankar on 05-Sep-11 09:03 AM
HI

heres my code if it helps
 
Imports System.IO
 
Public Class frmBrowser
 
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
txtFolders.Clear()
dlgBrowse.ShowDialog()
Dim di As New IO.DirectoryInfo(dlgBrowse.SelectedPath)
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
Dim fi As IO.FileInfo
For Each fi In aryFi
If txtFolders.Multiline = True Then
txtFolders.Text = txtFolders.Text & vbCrLf & fi.Name.Replace(fi.Extension, "")
Else
txtFolders.Text = txtFolders.Text & " | " & fi.Name.Replace(fi.Extension, "")
End If
Next
End Sub
 
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtFolders.Clear()
End Sub
 
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
dlgSave.DefaultExt = "*.txt"
dlgSave.AddExtension = True
dlgSave.FileName = ""
dlgSave.Filter = "Plain Text Files|*.txt|All Files|*.*"
If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(dlgSave.FileName, txtFolders.Text, True)
End If
End Sub
 
Private Sub txtFolders_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFolders.TextChanged
If txtFolders.Lines.Length >= 1 Then
btnSave.Enabled = True
btnReset.Enabled = True
Else
btnSave.Enabled = False
btnReset.Enabled = False
End If
End Sub
 
Private Sub cbxFilter_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxFilter.SelectedIndexChanged
 
End Sub
 
Private Sub lblSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSelected.Click
 
End Sub
 
Private Sub lblTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotal.Click
 
End Sub
End Class

refer
http://www.a1vbcode.com/a1vbcode/vbforums/Topic26390-3-1.aspx#bm26391
smr replied to abinav shankar on 05-Sep-11 09:04 AM
HI

use this

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()
 
    openFileDialog1.InitialDirectory = "c:\"
    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


refer
http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filter.aspx#Y798
Riley K replied to abinav shankar on 05-Sep-11 09:04 AM
Use OpenFIle dialog and pass the extension for Filter property

Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog
 
    openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
     
    openFileDialog1.Filter = "Word (*.doc) |*.doc;*.rtf|(*.txt) |*.txt|(*.*) |*.*"
 
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
      Console.WriteLine(openFileDialog1.FileName)
    End If


Cheers