Click here to Skip to main content
12,820,077 members (24,170 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 8 Jul 2010

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

, 8 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

            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>

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:

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")
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;)
MsgBox("Folder Created successfully")
End If

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


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


About the Author

Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 3 my reason for 3 is a well written ar... Pin
Simon_Whale14-Jul-10 2:42
memberSimon_Whale14-Jul-10 2:42 
GeneralReason for my vote of 1 0 Pin
kobymeir12-Jul-10 1:01
memberkobymeir12-Jul-10 1:01 
GeneralNot Sure this is Advanced Pin
TheyCallMeMrJames9-Jul-10 7:04
memberTheyCallMeMrJames9-Jul-10 7:04 
MD-MASTER13-Aug-12 8:35
memberMD-MASTER13-Aug-12 8:35 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 8 Jul 2010
Article Copyright 2010 by TechKidd
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid