C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: If the specified file does not exist, you will need to create it outside of the code to run the example.
Length: Once you obtain the FileInfo instance, you can access the Length property. This tells you how many bytes are in the file.
Here: We append to the file and then measure its size after the append operation.
VB.NET program that uses Length and FileInfo
Imports System.IO
Module Module1
Sub Main()
' Get file info for test.txt.
' ... Create this file in Visual Studio and select Copy If Newer on its properties.
Dim info As New FileInfo("test.txt")
' Get length of the file.
Dim length As Long = info.Length
' Add more characters to the file.
File.AppendAllText("test.txt", " More characters.")
' Get another file info.
' ... Then get the length.
Dim info2 As New FileInfo("test.txt")
Dim length2 As Long = info2.Length
' Show how the size changed.
Console.WriteLine("Before and after: {0}, {1}", length, length2)
Console.WriteLine("Size increase: {0}", length2 - length)
End Sub
End Module
Output
Before and after: 3, 20
Size increase: 17
Review: FileInfo is useful for acquiring file sizes. It does not require cumbersome loops or string conversions.