Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Creating a folder the advanced way [VB.Net Code]

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
8 Jul 2010CPOL2 min read 46.3K   4   4
This trick is on a intermediate/advanced level.
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)

alright explanation;

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.

Else
    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
    End If
End If


Lastly, add the end function code.
End Function


Full code:

''' <summary>
    ''' Create a Folder
    ''' </summary>
    ''' <param name="Path">The path of where the folder will be placed.</param>
    ''' <param name="FolderName">The name of the folder that you want to create.</param>
    ''' <param name="Attributes">The attributes of the folder like if you want it to be hidden or encrypted.</param>
    ''' <returns></returns>
    ''' <remarks>Tech Kid Creations Copyright 2010 (c)</remarks>
    Private Function CreateFolder(ByVal Path As String, ByVal FolderName As String, Optional ByVal Attributes As System.IO.FileAttributes = IO.FileAttributes.Normal)
        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.")
        ElseIf My.Computer.FileSystem.DirectoryExists(Path & "\" & FolderName) Then
            Throw New Exception("Could not create the folder because it already exists.")
        Else
            My.Computer.FileSystem.CreateDirectory(Path & "\" & FolderName)
            If Not Attributes = IO.FileAttributes.Normal Then
                My.Computer.FileSystem.GetDirectoryInfo(Path & "\" & FolderName).Attributes = Attributes
            End If
        End If
    End Function





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 1

Mirror 2

Mirror 3






RAR Archive Version Mirrors:
Mirror 1

Mirror 2

Mirror 3







**How to guide: Creating a test project with this tip.*
Defined folder:
Add a button(button1)
Add 2 textboxes(textbox1, textbox2)
Add a combobox(combobox1) set the dropdownstyle to dropdownlist

add the following items to combobox1:
IO.FileAttributes.Hidden
IO.FileAttributes.Archive
IO.FileAttributes.Encrypted
IO.FileAttributes.Normal
IO.FileAttributes.ReadOnly


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.")
else
msgbox("Folder Created successfully")
End If


put this code under the form load event:

combobox1.selectedindex = 0


Not Defined:
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(&quot;The folder creation failed.&quot;)
else
MsgBox("Folder Created successfully")
End If


And there you have it! Hope this trick helped!
- TechKid :-D

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 0 Pin
kobymeir12-Jul-10 0:01
kobymeir12-Jul-10 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.