Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below coding all the files in the selected folder are displayed, but I only want to display the .doc and .rtf files in the ListBox, kindly guide me the coding part , its urgent please.................

VB
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Windows.Forms

Public Class Form1
    Dim flb As New System.Windows.Forms.FolderBrowserDialog
    Dim fod As OpenFileDialog = New OpenFileDialog()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        flb.RootFolder = Environment.SpecialFolder.MyComputer
        flb.ShowNewFolderButton = False
        Dim result As DialogResult = flb.ShowDialog
        
        If (result = DialogResult.OK) Then
            ListBox1.Items.Clear()
            TextBox1.Text = flb.SelectedPath
            For Each filename In Directory.GetFiles(flb.SelectedPath)
             ListBox1.Items.Add(filename)
            Next
        End If

    End Sub
End Class
Posted
Updated 11-Nov-11 2:41am
v2
Comments
Maciej Los 11-Nov-11 8:52am    
Inside the For Each ... Next loop you need to check the extension of filename. Try to do it yourself!

Amend
VB
For Each filename In Directory.GetFiles(flb.SelectedPath)

to be
VB
For Each filename In Directory.GetFiles(flb.SelectedPath,"*.doc").Union(Directory.GetFiles(flb.SelectedPath, "*.rtf"))
 
Share this answer
 
You can filtre out the filenames that are .doc or .rtf with LinQ like this:-

VB
Using flb As New FolderBrowserDialog
           If flb.ShowDialog() = Windows.Forms.DialogResult.OK Then
               ListBox1.Items.Clear()
               TextBox1.Text = flb.SelectedPath
               Dim fileNames As String() = Directory.GetFiles(flb.SelectedPath)
               Dim selectedFiles As String() = From fileName In fileNames Where fileName.EndsWith(".rtf") Or fileName.EndsWith(".doc") Select fileName
               For Each filename In selectedFiles
                   ListBox1.Items.Add(filename)
               Next
           End If
       End Using


Hope this helps
 
Share this answer
 
use the filter to get only .DOC and .RTF files

dim files as string() = System.IO.Directory.GetFiles(path, "*.doc;*.rtf", SearchOption.AllDirectories)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900