How to VB.NET Directory operations

The Directory class in VB.NET empowers us with a multitude of capabilities, allowing us to perform essential operations such as creation, deletion, and movement of directories, among others. This versatile class simplifies the management and manipulation of directories within the VB.NET framework.

Directory class

One noteworthy advantage of utilizing the Directory class is its inherent static nature. Due to this characteristic, developers are relieved of the need to instantiate the class before accessing its methods. Instead, they can directly invoke the methods within the class from the Directory itself, streamlining the coding process and enhancing efficiency.

By using the static nature of the Directory class, VB.NET developers can seamlessly execute a wide range of operations, resulting in optimized workflow and code readability. The convenience and simplicity offered by the Directory class enable developers to achieve their objectives swiftly and effectively in the VB.NET environment.

In order to create a new directory , we can call CreateDirectory directly from Directory class.

  1. Syntax : Directory.CreateDirectory(DirPath)
  2. DirPath : The name of the new directory

VB.NET : Directory.CreateDirectory("c:\testdir")

How to check a directory exist or not ?

Before we creating a directory , we usually check that directory exist or not. For that we are using the Exists method in the Directory class.

Syntax : Directory.Exists(DirPath) as Boolean
  1. DirPath : The name of the directory
  2. 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 ?

If we want to move a directory and its contents from one location to another , we can use the Move method in the Directory class.

Syntax : Move(sourceDirName,destDirName)
  1. sourceDirName : The source directory we want to move.
  2. destDirName : The destinations 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)
  1. DirPath : The Directory we want to delete.

VB.NET : Directory.Delete("c:\testdir1")

The following VB.NET source code shows these operations :

Full Source VB.NET
Imports System.IO 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 ") Else 'create the directory testDir1 Directory.CreateDirectory("c:\testDir1") MsgBox("testDir1 created ! ") 'create the directory testDir2 Directory.CreateDirectory("c:\testDir1\testDir2") MsgBox("testDir2 created ! ") 'move the directory testDir2 as testDir in c: Directory.Move("c:\testDir1\testDir2", "c:\testDir") MsgBox("testDir2 moved ") 'delete the directory testDir1 Directory.Delete("c:\testDir1") MsgBox("testDir1 deleted ") End If End Sub End Class

When you executing 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 delete the directory testDir1. After the execution you can see testDir in c:\