FileInfo Properties In VB.NET

This article explains about various FileInfo properties in VB.NET
  • 13820

FileInfo Properties

The FileInfo class provides properties to get file name, extension, directory, size and file attributes. Here is a complete sample.

Imports System.Text

Imports System.IO

Module Module1

    Sub Main()

        ' Full file name

        Dim fileName As String = "C:\Temp\MaheshTXFI.txt"

        Dim fi As New IO.FileInfo(fileName)

        ' Create a new file

        Using fs As FileStream = fi.Create()

            Dim txt As [Byte]() = New UTF8Encoding(True).GetBytes("New file.")

            fs.Write(txt, 0, txt.Length)

            Dim author As [Byte]() = New UTF8Encoding(True).GetBytes("Author Mahesh Chand")

            fs.Write(author, 0, author.Length)

        End Using

        ' Get File Name

        Dim justFileName As String = fi.Name

        Console.WriteLine("File Name: {0}", justFileName)

        ' Get file name with full path

        Dim fullFileName As String = fi.FullName

        Console.WriteLine("File Name: {0}", fullFileName)

        ' Get file extension

        Dim extn As String = fi.Extension

        Console.WriteLine("File Extension: {0}", extn)

        ' Get directory name

        Dim directoryName As String = fi.DirectoryName

        Console.WriteLine("Directory Name: {0}", directoryName)

        ' File Exists ?

        Dim exists As Boolean = fi.Exists

        Console.WriteLine("File Exists: {0}", exists)

        If fi.Exists Then

            ' Get file size

            Dim size As Long = fi.Length

            Console.WriteLine("File Size in Bytes: {0}", size)

            ' File ReadOnly ?

            Dim IsReadOnly As Boolean = fi.IsReadOnly

            Console.WriteLine("Is ReadOnly: {0}", IsReadOnly)

            ' Creation, last access, and last write time

            Dim creationTime As DateTime = fi.CreationTime

            Console.WriteLine("Creation time: {0}", creationTime)

            Dim accessTime As DateTime = fi.LastAccessTime

            Console.WriteLine("Last access time: {0}", accessTime)

            Dim updatedTime As DateTime = fi.LastWriteTime

            Console.WriteLine("Last write time: {0}", updatedTime)

        End If



    End Sub

End Module


FileInfo Properties.jpg


More Articles

© 2020 DotNetHeaven. All rights reserved.