By: Issac Emailed: 1729 times Printed: 2331 times
Directory class in .net is of static nature we need not have to instantiate the class, and can direcly call methods in the class directly from the directory class.
How to create a directory?
In order to create a new directory, we can call CreateDirectory directly from Directory class.
Syntax: Directory.CreateDirectory (DirPath)
DirPath: The name of the new directory
VB.NET: Directory.CreateDirectory ("c:\testdir")
How to check a directory exist or not?
Before we create a directory, we usually check whether that directory exist or not. For that we are using the Exists method in the Directory class.
Syntax : Directory.Exists(DirPath) as Boolean
DirPath : The name of the directory
Boolean : Returns true or false , if directory exist it Returns true , else it Returns false
VB.NET: Directory.Exists ("c:\testdir")
How to move a Directory?
We can use Move method in the Directory class to move a directory and its contents from one location to another
Syntax : Move(sourceDirName,destDirName)
sourceDirName : The source directory we are going to move.
destDirName : The destination directory name.
VB.NET: Directory.Move ("c:\testdir1\testdir2", "c:\testdir")
How to delete a Directory ?
When we want to delete a directory we can use the Delete method in the Directory class
Syntax : Delete(DirPath)
DirPath : The Directory we want to delete.
VB.NET : Directory.Delete("c:\testdir1")
The following VB.NET source code shows these operations :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button1.Click
If Directory.Exists("c:\testDir1") Then
'shows message if testdir1 exist
MsgBox("Directory 'testDir' Exist ")
'create the directory testDir1
MsgBox("testDir1 created ! ")
'create the directory testDir2
MsgBox("testDir2 created ! ")
'move the directory testDir2 as testDir in c:\
MsgBox("testDir2 moved ")
'delete the directory testDir1
MsgBox("testDir1 deleted ")
When you run this program you can see, first it create directory testDir1 and then testDir2 is creating inside testDir1, Next the program move the testDir2 to testDir. Finally it deletes the directory testDir1. After the execution you can see testDir in c:\
Be the first one to add a comment
Most Viewed Articles (in VB.net )
Most Emailed Articles (in VB.net)