FileInfo class in VB.NET

This article will explain about the FileInfo class and its use.
  • 2281

Introduction

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

FileInfo Class

The FileInfo class does not have static methods and can only be used on instantiated objects. The FileInfo object represents a file on a disk or network location. OR Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. Some of the most useful methods of the FileInfo class are:

List of Methods of FileInfo Class, I have included only a few Methods of FileInfo Class.

S.No Methods Description
1 Create
 
This method is used to create the new file.
2 CreateText This Method Creates a StreamWriter that writes a new text file.
3 Delete This method is used to delete a existing file.
4 CopyTo The CopyTo method is used to copy the existing file into new file. 
5 MoveTo The MoveTo method is used to move the file one place to another valid location.
6 AppendText This method creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
7 OpenText This method creates a StreamReader with UTF8 encoding that reads from an existing text file.

Figure 1 â€" FileInfo Methods


The methods of the FileInfo class.

Create Method

This method is used to create the new file.

Imports System.IO

Module Module1

   Sub Main()

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

       Dim fl As New FileInfo(path)

       File.Create(path)

       If True Then

           Console.WriteLine("File has been created")

       End If

   End Sub

End Module

CreateText

This Method Creates a StreamWriter that writes a new text file.

Imports System.IO

Module Module1

   Sub Main()

       Dim fi As New FileInfo("D:\MyTestFilecreatetext1.txt")

       Dim str As StreamWriter = fi.CreateText()

        str.WriteLine("hello")

       Console.WriteLine("File has been created with text")

        str.Close()

   End Sub

End Module

Delete

This method is used to delete a existing file.

Imports System.IO

Module Module1

   Sub Main()

      Dim fi As New FileInfo("D:\MyTestFilecreatetext.txt")

        fi.Delete()

       Console.WriteLine("File has been deleted")

   End Sub

End Module

CopyTo

The CopyTo method is used to copy the existing file into new file. 

Imports System.IO

Module Module1

   Sub Main()

       Dim path As String = "d:\MyTestFile7.txt"

       Dim path2 As String = "d:\NewFile.txt"

       Dim fi1 As New FileInfo(path)

       Dim fi2 As New FileInfo(path2)

        fi1.CopyTo(path2)

       Console.WriteLine("{0} was copied to {1}.", path, path2)

   End Sub

End Module

MoveTo

The MoveTo method is used to move the file one place to another valid location.

Imports System.IO

Module Module1

   Sub Main()

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

       Dim path2 As String = "E:\NewFile1.txt"

       Dim fi1 As New FileInfo(path)

       Dim fi2 As New FileInfo(path2)

        fi1.MoveTo(path2)

       Console.WriteLine("{0} was moved to {1}.", path, path2)

   End Sub

End Module

AppendText

This method creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.

Imports System.IO

Module Module1

   Sub Main()

       Dim fi As New FileInfo("E:\NewFile1.txt")

       Dim sw As StreamWriter = fi.AppendText()

        sw.WriteLine("This")

        sw.WriteLine("is Extra")

        sw.WriteLine("Text")

       Console.WriteLine("File has been appended")

        sw.Close()

   End Sub

End Module

 

Example

 

Imports System.IO

Module Module1

    Sub Main()

 

        ' Create Method

 

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

        Dim fl As New FileInfo(path)

        File.Create(path)

        If True Then

            Console.WriteLine("File has been created")

        End If

 

        'CreateText Method

 

        Dim fi As New FileInfo("D:\MyTestFilecreatetext1.txt")

        Dim str As StreamWriter = fi.CreateText()

        str.WriteLine("hello")

        Console.WriteLine("File has been created with text")

        str.Close()

 

 

        'Delete Method

 

        Dim fi As New FileInfo("D:\MyTestFilecreatetext.txt")

        fi.Delete()

        Console.WriteLine("File has been deleted")

 

        'CreateText Method

 

        Dim path As String = "d:\MyTestFile7.txt"

        Dim path2 As String = "d:\NewFile.txt"

        Dim fi1 As New FileInfo(path)

        Dim fi2 As New FileInfo(path2)

        fi1.CopyTo(path2)

        Console.WriteLine("{0} was copied to {1}.", path, path2)

 

        'MoveTo Method

 

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

        Dim path2 As String = "E:\NewFile1.txt"

        Dim fi1 As New FileInfo(path)

        Dim fi2 As New FileInfo(path2)

        fi1.MoveTo(path2)

        Console.WriteLine("{0} was moved to {1}.", path, path2)

 

        'ApendText Method

 

        Dim fi As New FileInfo("E:\NewFile1.txt")

        Dim sw As StreamWriter = fi.AppendText()

        sw.WriteLine("This")

        sw.WriteLine("is Extra")

        sw.WriteLine("Text")

        Console.WriteLine("File has been appended")

        sw.Close()

    End Sub

End Module

 

The FileInfo class provides the following properties that enable you to retrieve information about a file.
 

S.No Property Description
1 CreationTime This property returns the creation of the file date and time.
2 Exists The Exists property checks for the presence of a file before operating on it.
3 Extension It returns the type of the file in the extension name
4 FullName It returns the full name of the file from the root directory.
5 Name The Name retrieves the name of a file.
6 LastWriteTime It returns the last file saving date and time
7 Length The Length retrieves the size of a file.

Figure 2 â€" FileInfo Properties

Property - Example

Imports System.IO

Module Module1

   Sub Main()

       Dim fi As New FileInfo("E:\NewFile1.txt")

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

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

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

       Console.WriteLine("File length is {0} ", fi.Length.ToString() &" Bytes")

        Console.WriteLine("File extension is {0} ", fi.Extension)

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

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

   End Sub

End Module

 

Conclusion:

The FileInfo 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

© 2019 DotNetHeaven. All rights reserved.