List Files in Visual Basic .Net / Visual Basic 2005
The Visual Basic 6.0 DirListBox control has been rendered obsolete by the OpenFileDialog and SaveFileDialog components in Visual Basic 2005.
The Visual Basic 6.0 DirListBox control was typically used to display directories and paths in a File Open or Save dialog box.
In Visual Basic 2005, the Windows Forms OpenFileDialog and SaveFileDialog components provide the ability to create standard Windows dialog boxes for working with files, in most cases eliminating the need for the DirListBox control.
For this example, Let us have a form with a TextBox and a command button. When the command buton is pressed, the folderdialog is shown and then the selected folder is displayed in the textbox
Add the FolderBrowser Dialog to the form from the Dialogs Collection (see below).
This control will not be placed on the form but on a separate tray at the bottom of the Windows Forms Designer. (see below)
Now in the click event for the Button have the following code:
Private Sub BtnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFile.Click
Dim MyFileOpen As New System.Windows.Forms.OpenFileDialog
Dim bExOccured As Boolean
Dim retVal As DialogResult
Dim sMyFile As String
' does not add an extension to a file name if the user omits the extension
MyFileOpen.AddExtension = True
'dialog box does not allow multiple files to be selected
MyFileOpen.Multiselect = False
MyFileOpen.Filter = "ASCII files (*.txt;*.log)|*.txt;*.log"
retVal = MyFileOpen.ShowDialog()
If retVal = Windows.Forms.DialogResult.OK Then
If MyFileOpen.CheckFileExists = True And MyFileOpen.CheckPathExists = True Then
sMyFile = MyFileOpen.FileName
Catch ex1 As AccessViolationException
bExOccured = True
Catch ex As Exception
bExOccured = True
If bExOccured = True Then
MsgBox("Program executed with some errors!!!")
You can use filters to restrict the type of files that can be opened. A sample of common filters is given below
'MyFileOpen.Filter = "Microsoft Word Documents (*.doc)|*.doc|Microsoft Word Documents (*.rtf)|*.rtf"
'MyFileOpen.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF"
'MyFileOpen.Filter = "Microsoft Word Documents (*.doc;*.rtf)|*.doc;*.rtf"
'MyFileOpen.Filter = "Microsoft Excel Workbooks (*.xls)|*.xls"
'MyFileOpen.Filter = "Microsoft Excel Addins (*.xla;*.xll)|*.xla;*.xll"
'MyFileOpen.Filter = "All files (*.*)|*.*"
'MyFileOpen.Filter = "Text files (*.txt)|*.txt"
This class allows you to check whether a file exists and to open it. The ShowReadOnly property determines whether a read-only check box appears in the dialog box. The ReadOnlyChecked property indicates whether the read-only check box is checked.
Most of the functionality for this class is found in the FileDialog class.
Microsoft recommends that you use the OpenFileDialog and SaveFileDialog components to provide a consistent and familiar user experience. If you find it necessary to create your own file dialog boxes, Visual Basic 2005 does provide a DirListBox control as part of the Microsoft Visual Basic Compatibility Runtime library.
For CommonDialog implementation in VB6.0 refer http://vbadud.blogspot.com/2007/06/visual-basic-common-dialog.html
When a Visual Basic 6.0 application is upgraded to Visual Basic 2005, any existing DirListBox controls are upgraded to the VB6.DirListBox control that is provided as a part of the compatibility library (Microsoft.VisualBasic.Compatibility).