Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Browsing files in a folder using VB.NET


Browsing files in a folder using VB.NET

This small piece of code will give an idea to browse file in a given folder.

Start your VB.NET application and create a new project (Ctrl+Shift+N) in Windows Application and design your form as you like. I have design my form as below (Figure 1.):

Browsing files in a folder using VB.NET

Now drag a FolderBrowseDialog from Toolbox and name it FolderBrowserDialog1.

On click event of btnBrowse open FolderBrowserDialog1 to select folder and then assign that path to txtPath.

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click  
FolderBrowserDialog1.ShowDialog() 
txtPath.Text = FolderBrowserDialog1.SelectedPath  
End Sub

Now on Click event of btnSearch we have to search test pattern. Following piece of code will do it for you.

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click  
lstSeachResult.Items.Clear()  
Dim dir_info As New DirectoryInfo(txtPath.Text)  
Dim file_infos() As FileInfo  
file_infos = dir_info.GetFiles(txtsearch.Text)  
For Each file_info As FileInfo In file_infos  
lstSeachResult.Items.Add(file_info.Name)  
Next file_info  
End Sub

Now your complete code will look like as below:

Imports System.IO  
 
Public Class Form1
 
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
 
FolderBrowserDialog1.ShowDialog()
 
txtPath.Text = FolderBrowserDialog1.SelectedPath
 
End Sub
 
 
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
 
lstSeachResult.Items.Clear()
 
Dim dir_info As New DirectoryInfo(txtPath.Text)
 
Dim file_infos() As FileInfo
 
file_infos = dir_info.GetFiles(txtsearch.Text)
 
For Each file_info As FileInfo In file_infos
 
lstSeachResult.Items.Add(file_info.Name)
 
Next file_info
 
End Sub

Note:

Here you can search files with pattern like:
 
 
*.* for search all files
 
*.js files ends with .js (JavaScript files)
 
*.vb file ends with .vb (visual basic files
 
*sys* files which contain text ‘sys’

Download

Download source code for ‘Browsing files in a folder using VB.NET’

Download

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Easy and excellent code
Thanks dear Friend

Comment by NarasimhaRaju.P on October 15, 2011 @ 6:41 am

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image