Hello viewers, I'm TechKid From TechKid Creations.
This is my first article/tip on this forum so please take it easy on me. :D
So I'm going to share with you a code that I made about creating folders.
The function that I made is basicly just a quicker way to achieve this than using just the My.Computer.CreateDirectory statement.
so lets start.
start by copying and pasting this code.
Private Function CreateFolder(ByVal Path As String, ByVal FolderName As String, Optional ByVal Attributes As System.IO.FileAttributes = IO.FileAttributes.Normal)
ByVal Path As String
: The path that you want to create a folder in.
ByVal FolderName As String
: The name of the folder you want to create.
Optional ByVal Attributes As System.IO.FileAttributes = IO.FileAttributes.Normal
: The attributes to add to the folder after creating it. So ex: Hide the folder, encrypt it etc. and since its an optional ByVal the default is set to normal (no attributes will be added)
Now that we have that laid out, lets begin the actual coding part.
First we check if the path exists, if it does throw an exception.
If My.Computer.FileSystem.DirectoryExists(Path) = False Then
Throw New Exception("The specified path does not exist. Make sure the specified path has been spelled correctly.")
If the path does exist we move on the checking if the folder that the user is trying to create already exists, if it does throw an exception.
ElseIf My.Computer.FileSystem.DirectoryExists(Path & "\" & FolderName) Then
Throw New Exception("Could not create the folder because it already exists.")
If the path exists and the folder doesn't then create it.
My.Computer.FileSystem.CreateDirectory(Path & "\" & FolderName)
Now check if the 'Attributes' value has been changed, if it has add the attributes stored in the value 'Attributes'.
If Not Attributes = IO.FileAttributes.Normal Then
My.Computer.FileSystem.GetDirectoryInfo(Path & "\" & FolderName).Attributes = Attributes
Lastly, add the end function code.
''' Create a Folder
''' <param name="Path">The path of where the folder will be placed.</param>
To test if the code works you can create a test project (Look below for a how to guide)or you can download a test project made by me.
These downloads also include moving and deleting folders.
ZIP Archive Version Mirrors:Mirror 1Mirror 2Mirror 3
RAR Archive Version Mirrors:Mirror 1Mirror 2Mirror 3
**How to guide: Creating a test project with this tip.*
Add a button(button1)
Add 2 textboxes(textbox1, textbox2)
Add a combobox(combobox1) set the dropdownstyle to dropdownlist
add the following items to combobox1:
Textbox1 will store the path and textbox2 will store the folder name.
under the button1 click event:
CreateFolder(textbox1.text, textbox2.text, combobox1.selecteditem)
Optional: to check if the folder was created
put this code under the "CreateFolder" statement in the button1 click event:
if Not My.Computer.FileSystem.DirectoryExists(textbox1.text & "\" & textbox2.text) Then
Throw New Exception("The folder creation failed.")
msgbox("Folder Created successfully")
put this code under the form load event:
combobox1.selectedindex = 0
Add a button to your form
in the button1 click event put this code in:
CreateFolder("C:/","TestFolder",IO.FileAttributes.Hidden + IO.FileAttributes.Encrypted)
Optional: Check if the folder got created;
Under the 'CreateFolder' statement put:
if Not My.Computer.FileSystem.DirectoryExists("C:/TestFolder") Then
MsgBox("The folder creation failed.")
MsgBox("Folder Created successfully")
And there you have it! Hope this trick helped!
- TechKid :-D