Directory class in VB.NET

This article will explain about the Directory class and its operations.
  • 4326

Introduction

This article will explain about the Directory class and its operations. The System.IO namespace is one of the most significant namespaces used for working with directory in the .Net Framework. Let us see about the class.

Directory Class

The Directory class exposes many static methods for moving, copying, and deleting directories. There are static methods that involve createdirectory, moving and deleting directories.

Some of the most useful static methods of the Directory class are:

 
S.No Method Description
1 CreateDirectory This method is used to create a directory in the specified path.
2 Delete This method is used to Delete a directory.
3 Exists It returns whether the directory exists or not as Boolean result.
4 Move Moves a specified directory to a new location. we can specify a different name for the file in the new location.
5 Getfiles Return an array of files objects in the current directory.
6 GetDirectories Returns an array of directory objects that represent the directories below the current directory.
7 GetCreationTime Gets the creation date and time of a directory.

CreateDirectory Method

This method is used to create a directory in the specified path.

Imports System.IO

Module Module1

   Sub Main()

       Dim dirpath As String = "D:\Testdir"

        Try

           If Directory.Exists(dirpath)Then

               Console.WriteLine("That path exists already.")

                Return

           End If

 

           Directory.CreateDirectory(dirpath)

           Console.WriteLine("The directory was created successfully at {0}.",Directory.GetCreationTime(dirpath))

       Catch e As Exception

           Console.WriteLine("The process failed: {0}", e.ToString())

       End Try

   End Sub

End Module

Delete Method

This method is used to Delete a directory.

Imports System.IO

Module Module1

   Sub Main()

       Dim dirpath As String = "D:\Testdir"

        Try

           Directory.Delete(dirpath)

           Console.WriteLine("The directory was deleted successfully.")

       Catch e As Exception

           Console.WriteLine("The process failed: {0}", e.ToString())

       End Try

   End Sub

End Module

Exists Method

It returns whether the directory exists or not as Boolean result.

Imports System.IO

Module Module1

   Sub Main()

       Dim dirpath As String = "D:\Testdir"

       Try

           If Directory.Exists(dirpath)Then

               Console.WriteLine("That path exists already.")

               Return

           End If

       Catch e As Exception

           Console.WriteLine("The process failed: {0}", e.ToString())

       End Try

   End Sub

End Module

Move Method

This method is used to Move a specified directory to a new location. we can specify a different name for the file in the new location.

Imports System.IO

Module Module1

   Sub Main()

       Dim path As String = "e:\MyTestFile1.txt"

       Dim path1 As String = "e:\MyTest1.txt"

       Directory.Move(path, path1)

       Console.WriteLine("File has been moved")

   End Sub

End Module

 

GetCreationTime Method

 

This method is used to create date and time of a directory.

 

Imports System.IO

Module Module1

   Sub Main()

       Dim dirpath As String = "D:\Testdir4"

       If True Then

           Try

               If Directory.Exists(dirpath)Then

                   Console.WriteLine("That path exists already.")

                   Return

               End If

 

               Directory.CreateDirectory(dirpath)

               Console.WriteLine("The directory was created successfully at {0}.",Directory.GetCreationTime(dirpath))

 

           Catch e As Exception

               Console.WriteLine("The process failed: {0}", e.ToString())

           End Try

       End If

   End Sub

End Module

Getfiles Method

This method is used to return an array of files objects in the current directory.

GetDirectories Method

This is used to returns an array of directory objects that represent the directories below the current directory.

Conclusion:

The File class is very useful for working with files. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.