• We just launched and are currently in beta. Join us as we build and grow the community.

File Manager in Visual Basic

Goodnight

Play-to-Earn Innovator
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
55
Likes
161
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Introduction:
This tutorial is on how to create a file manager in Visual Basic.

Design:
This program requires the following:
{component} {name} {text} {use}
Button button1 Open To open the directory or file selected.
Button button2 Root To reset to the C:\ (drive).
Button button3 Delete To delete the selected directory or file.
Listbox listbox1 - To list the directories and files held within the current directory the user is browsing.

Variables:
We need to keep track of the current path of the listbox so we need to create and use a string, lets name it 'path'...

  1. Dim

    path As

    String

    = "C:\"

It has the default value of the root Windows C: drive path (C:\).

Open:
Now for the open button. First we check if the selected item within the listbox is a directory, if it is we set the path appropriately and run a new function we have not created yet named 'updateListbox'. If it is a file that is selected, we open it with the default program set on the computer...

  1. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  2. If

    Not

    (ListBox1.SelectedItem = Nothing

    ) Then
  3. If

    (My.Computer.FileSystem.DirectoryExists(path + ListBox1.SelectedItem)) Then
  4. path += ListBox1.SelectedItem + "\"
  5. updateListbox()
  6. ElseIf

    (My.Computer.FileSystem.FileExists(path + ListBox1.SelectedItem)) Then
  7. Diagnostics.Process.Start(path + ListBox1.SelectedItem)
  8. End

    If
  9. End

    If
  10. End

    Sub

updateListbox:
Now for the function we already call from the open button, button1. It simply iterates through each directory and file held within the computers filesystem at the current path and adds each one to the listbox. Of course we delete all the currently held items first....

  1. Private

    Function

    updateListbox()
  2. ListBox1.Items.Clear()
  3. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetDirectories(path)
  4. ListBox1.Items.Add(f.Split("\"

    )(f.Split("\"

    ).Count() - 1))
  5. Next
  6. For

    Each

    f As

    String

    In

    My.Computer.FileSystem.GetFiles(path)
  7. ListBox1.Items.Add(f.Split("\"

    )(f.Split("\"

    ).Count() - 1))
  8. Next
  9. End

    Function

Root:
The root button is simple. We simply set the path to the default Windows path (C:\, the C drive) and run the 'updateListbox' function...

  1. Private

    Sub

    Button3_Click(sender As

    Object

    , e As

    EventArgs) Handles Button3.Click
  2. path = "C:\"
  3. updateListbox()
  4. End

    Sub

Delete:
For the delete button function we check again whether the selected item is a directory or file and use the appropriate function within the my.computer.filesystem module path to delete it. If it is a directory, we use the mode fileIO.deleteDirectoryOption.deleteAllContents) to clear the entire directory; there are other options available and we could ask the user to confirm whether they want to delete the directory or not...

  1. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  2. Dim

    delPath As

    String

    = path + ListBox1.SelectedItem
  3. If

    (My.Computer.FileSystem.DirectoryExists(delPath)) Then
  4. My.Computer.FileSystem.DeleteDirectory(delPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
  5. ElseIf

    (My.Computer.FileSystem.FileExists(delPath)) Then
  6. My.Computer.FileSystem.DeleteFile(delPath)
  7. End

    If
  8. End

    Sub

 

452,496

332,845

332,853

Top