Goodnight
Play-to-Earn Innovator
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'...
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...
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....
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...
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...
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'...
- 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...
- Private
Sub
Button1_Click(sender As
Object
, e As
EventArgs) Handles Button1.Click
- If
Not
(ListBox1.SelectedItem = Nothing
) Then
- If
(My.Computer.FileSystem.DirectoryExists(path + ListBox1.SelectedItem)) Then
- path += ListBox1.SelectedItem + "\"
- updateListbox()
- ElseIf
(My.Computer.FileSystem.FileExists(path + ListBox1.SelectedItem)) Then
- Diagnostics.Process.Start(path + ListBox1.SelectedItem)
- End
If
- End
If
- 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....
- Private
Function
updateListbox()
- ListBox1.Items.Clear()
- For
Each
f As
String
In
My.Computer.FileSystem.GetDirectories(path)
- ListBox1.Items.Add(f.Split("\"
)(f.Split("\"
).Count() - 1))
- Next
- For
Each
f As
String
In
My.Computer.FileSystem.GetFiles(path)
- ListBox1.Items.Add(f.Split("\"
)(f.Split("\"
).Count() - 1))
- Next
- 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...
- Private
Sub
Button3_Click(sender As
Object
, e As
EventArgs) Handles Button3.Click
- path = "C:\"
- updateListbox()
- 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...
- Private
Sub
Button2_Click(sender As
Object
, e As
EventArgs) Handles Button2.Click
- Dim
delPath As
String
= path + ListBox1.SelectedItem
- If
(My.Computer.FileSystem.DirectoryExists(delPath)) Then
- My.Computer.FileSystem.DeleteDirectory(delPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
- ElseIf
(My.Computer.FileSystem.FileExists(delPath)) Then
- My.Computer.FileSystem.DeleteFile(delPath)
- End
If
- End
Sub