DirectoryInfo Class in VB.NET

This article will explain the DirectoryInfo class and its uses.
  • 4539

Introduction

This article will explain the DirectoryInfo class and its use. 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.

DirectoryInfo Class

The DirectoryInfo class shares almost all of the same properties as the FileInfo class, except that they operate on directories not files. The DirectoryInfo class does not have static methods and can only be used on instantiated objects. The DirectoryInfo object represents a physical directory on a disk. It also provides instance methods for the delete, create new directory and sub directories.  

When we use DirectoryInfo class

If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class.

Some of the most useful methods of the DirectoryInfo class are:

S.No Methods Description
1 Create This method is used to create the new directory.
2 CreateSubdirectory This method is used to create a subdirectory or subdirectories on the specified path.
3 MoveTo Moves a DirectoryInfo instance and its contents to a new path.
4 Delete Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.
5 GetDirectories
 
This method is used to get the sub directories in the given directory path.
6 GetFiles
 
The GetFiles method is used to get the files in the specified folder.

Figure1 DirectoryInfo Methods

Now Let us see the some of the methods in the DirectoryInfo class.

Create Method

This method is used to create the new directory.

Dim path As [String] = "D:\MyTestFile1.txt"

       Dim fl As New DirectoryInfo(path)

        fl.Create()

       If True Then

           Console.WriteLine("Directory has been created")

       End If

 

CreateSubDirectory Method

 

This method is used to create a subdirectory or subdirectories on the specified path.

 

Dim path As [String] = "D:\MyTestFile1.txt"

       Dim fl As New DirectoryInfo(path)

       Dim dis As DirectoryInfo = fl.CreateSubdirectory("hellotest")

       If True Then

           Console.WriteLine("SubDirectory has been created")

       End If

 

 

MoveTo Method

 

Moves a DirectoryInfo instance and its contents to a new path.

 

Dim path As [String] = "D:\MyTestFile1.txt"

       Dim path1 As String = "D:\NewFile1.txt"

       Dim f1 As New DirectoryInfo(path)

       Dim f2 As New DirectoryInfo(path1)

        f1.MoveTo(path1)

       If True Then

           Console.WriteLine("Directory has been Moved")

       End If

 

Delete Method

 

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

 

Dim path As String = "D:\NewFile1.txt"

       Dim f1 As New DirectoryInfo(path)

        f1.Delete()

       If True Then

           Console.WriteLine("Directory has been deleted")

       End If

 

GetDirectories Method

 

This method is used to get the sub directories in the given directory path or, Returns the subdirectories of the current directory.

 

Try

 Dim di As New DirectoryInfo("D:\newFile\")

 Dim dir1 As DirectoryInfo() = di.GetDirectories()

  Console.WriteLine("The number of directories containing is {0}.", dir1.Length)

       Catch e As Exception

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

       End Try

 

GetFiles

 

The GetFiles method is used to get the files in the specified folder.

 

Dim di As New DirectoryInfo("D:\newfile")

       Dim dirs As DirectoryInfo() = di.GetDirectories()

       For Each diNext As DirectoryInfo In dirs

           Console.WriteLine("The number of files in {0} is {1}", diNext, diNext.GetFiles().Length)

       Next

 

Example

 

Imports System.IO

Module Module1

 

    Sub Main()

        'Create(Method)

 

        Dim path As [String] = "D:\MyTestFile1.txt"

        Dim fl As New DirectoryInfo(path)

        fl.Create()

        If True Then

            Console.WriteLine("Directory has been created")

        End If

 

        'CreateSubdirectory(Method)

 

        Dim path As [String] = "D:\MyTestFile1.txt"

        Dim fl As New DirectoryInfo(path)

        Dim dis As DirectoryInfo = fl.CreateSubdirectory("hellotest")

        If True Then

            Console.WriteLine("SubDirectory has been created")

        End If

 

        'MoveTo(Method)

 

        Dim path As [String] = "D:\MyTestFile1.txt"

        Dim path1 As String = "D:\NewFile1.txt"

        Dim f1 As New DirectoryInfo(path)

        Dim f2 As New DirectoryInfo(path1)

        f1.MoveTo(path1)

        If True Then

            Console.WriteLine("Directory has been Moved")

        End If

 

        ' Delete(Method)

 

        Dim path As String = "D:\NewFile1.txt"

        Dim f1 As New DirectoryInfo(path)

        f1.Delete()

        If True Then

            Console.WriteLine("Directory has been deleted")

        End If

 

        'GetDirectories(method)

 

        Try

            Dim di As New DirectoryInfo("D:\newFile\")

            Dim dir1 As DirectoryInfo() = di.GetDirectories()

            Console.WriteLine("The number of directories containing is {0}.", dir1.Length)

        Catch e As Exception

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

        End Try

 

        'GetFiles(method)

 

        Dim di As New DirectoryInfo("D:\newfile")

        Dim dirs As DirectoryInfo() = di.GetDirectories()

        For Each diNext As DirectoryInfo In dirs

            Console.WriteLine("The number of files in {0} is {1}", diNext, diNext.GetFiles().Length)

        Next

    End Sub

End Module

 

The DirectoryInfo class provides the following properties that enable you to retrieve information about a directory.

 

S.No Property Description
1 CreationTime This property returns the creation of the directory date and time.
2 Exists It returns whether the directory exists or not as Boolean result.
3 FullName It returns the full name of the file from the root directory.
4 Name It returns the name of the directory
5 LastAccessTime It returns the date and time of the last access time
6 LastWriteTime It returns the last file saving date and time
7 Root Gets the root portion of a path.

Figure 2  DirectoryInfo Properties

Property - Example

Imports System.IO

Module Module1

 

   Sub Main()

       Dim fi As New DirectoryInfo("D:\newfile")

       Console.WriteLine("Directory name is {0} ", fi.Name)

       Console.WriteLine("Directory creation time is {0} ", fi.CreationTime.ToLongTimeString())

       Console.WriteLine("Directory Lastaccesstime is {0} ", fi.LastAccessTime.ToLongDateString())

       Console.WriteLine("Directory exist is: ", fi.Exists)

       Console.WriteLine("Directory LastWriteTime is {0} ", fi.LastWriteTime)

       Console.WriteLine("Directory root is {0} ", fi.Root)

   End Sub

End Module

 

Conclusion:

The DirectoryInfo class is very useful for working with directories. 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

© 2019 DotNetHeaven. All rights reserved.