How to Create a File Browser in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 3085 times.

Introduction:
Welcome to a tutorial on how to create a file browser in Visual Basic.

Steps of Creation:
Step 1:

First we want to create a form with a listbox to contain the current path items (files and directories), a button to open a directory and a button to go up a level.

We also want to import System.IO so we can access our files, directories and paths.

  1. Imports System.IO

Step 2:
So, first lets make a string variable to contain the current viewing path. And a function to update the listbox with the current paths directories and files.

  1. Dim curPath As String = "C:\"
  2.  
  3. Private Function updateListbox()
  4. ListBox1.Items.Clear()
  5. Dim di As New DirectoryInfo(curPath)
  6. Dim diArr As DirectoryInfo() = di.GetDirectories()
  7. Dim dri As DirectoryInfo
  8. For Each dri In diArr
  9. ListBox1.Items.Add("Dir: " & dri.Name)
  10. Next dri
  11. For Each foundFile As String In My.Computer.FileSystem.GetFiles(curPath)
  12. ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  13. Next
  14. End Function

Step 3:
Because I will be making mine say "Dir: " in front of directory names I will need to make a function to remove the front of the name, so here it is if you want to do the same...

  1. Private Function removeFront(ByVal s As String)
  2. Dim r As String = Nothing
  3. For i As Integer = 0 To s.Count() - 1
  4. If (i > 4) Then r &= s.Chars(i)
  5. Next
  6. Return r
  7. End Function

Step 4:
Now we have our functions to deal with the paths... lets make it automatically load our C:\ directories and files when the form loads...

  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. updateListbox()
  3. End Sub

Step 5:
Next, lets make our Go button. It will simply check if the selected item starts with "Dir: " and if it does it will change directory, otherwise it will start the file.

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. If (Not ListBox1.SelectedItem = Nothing) Then
  3. If (ListBox1.SelectedItem.ToString().StartsWith("Dir: ")) Then
  4. Dim dir As String = removeFront(ListBox1.SelectedItem.ToString())
  5. curPath = curPath & dir & "\"
  6. updateListbox()
  7. Else
  8. MsgBox(curPath & ListBox1.SelectedItem.ToString())
  9. Diagnostics.Process.Start(curPath & ListBox1.SelectedItem.ToString())
  10. End If
  11. End If
  12. End Sub

Step 6:
Finally lets make our level up button. It simply gets the previous directory from the current path by splitting it by the "\" backslash.

  1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2. Dim pathParts As String() = curPath.Split("\")
  3. Dim backPath As String = Nothing
  4. For Each part As String In pathParts
  5. If (Not part = pathParts(pathParts.Count() - 2)) Then backPath &= part & "\"
  6. Next
  7. If (backPath.EndsWith("\\")) Then backPath = backPath.Remove(backPath.Count() - 1)
  8. curPath = backPath
  9. updateListbox()
  10. End Sub

Project Complete!
That's it! Below is the full source code and download to the project files.

  1. Imports System.IO
  2. Public Class Form1
  3. Dim curPath As String = "C:\"
  4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  5. updateListbox()
  6. End Sub
  7.  
  8. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  9. If (Not ListBox1.SelectedItem = Nothing) Then
  10. If (ListBox1.SelectedItem.ToString().StartsWith("Dir: ")) Then
  11. Dim dir As String = removeFront(ListBox1.SelectedItem.ToString())
  12. curPath = curPath & dir & "\"
  13. updateListbox()
  14. Else
  15. MsgBox(curPath & ListBox1.SelectedItem.ToString())
  16. Diagnostics.Process.Start(curPath & ListBox1.SelectedItem.ToString())
  17. End If
  18. End If
  19. End Sub
  20.  
  21. Private Function removeFront(ByVal s As String)
  22. Dim r As String = Nothing
  23. For i As Integer = 0 To s.Count() - 1
  24. If (i > 4) Then r &= s.Chars(i)
  25. Next
  26. Return r
  27. End Function
  28.  
  29. Private Function updateListbox()
  30. ListBox1.Items.Clear()
  31. Dim di As New DirectoryInfo(curPath)
  32. Dim diArr As DirectoryInfo() = di.GetDirectories()
  33. Dim dri As DirectoryInfo
  34. For Each dri In diArr
  35. ListBox1.Items.Add("Dir: " & dri.Name)
  36. Next dri
  37. For Each foundFile As String In My.Computer.FileSystem.GetFiles(curPath)
  38. ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  39. Next
  40. End Function
  41.  
  42. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  43. Dim pathParts As String() = curPath.Split("\")
  44. Dim backPath As String = Nothing
  45. For Each part As String In pathParts
  46. If (Not part = pathParts(pathParts.Count() - 2)) Then backPath &= part & "\"
  47. Next
  48. If (backPath.EndsWith("\\")) Then backPath = backPath.Remove(backPath.Count() - 1)
  49. curPath = backPath
  50. updateListbox()
  51. End Sub
  52. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.