C# Get File Size
c# get file size or length File management or manipulation requires calculating the size of file or moving file or deleting it. FileInfo is a class provided in .net namespace System.IO which can do those operations. FileInfo class has all those methods and properties to calculate the size of file or delete the file or move the file.
C# program to get size of file.
This example gets the size of file stored at my local c:\ drive
using System;
using System.IO;
namespace GET.File.Size.Example
{
class Program
{
static void Main()
{
var fi = new FileInfo(@"c:\SQLManagementStudio_x86_ENU.exe");
long size = fi.Length;
Console.WriteLine(size.ToString());
Console.ReadLine();
}
}
}
VB.net program to get size of file
Imports System.IO
Namespace GET.File.Size.Example
Class Program
Private Shared Sub Main()
Dim fi = New FileInfo("c:\SQLManagementStudio_x86_ENU.exe")
Dim size As Long = fi.Length
Console.WriteLine(size.ToString())
Console.ReadLine()
End Sub
End Class
End Namespace
Output
Output: 176440672 (bytes)
Description fi holds the reference of FileInfo instance. The constructor of fileInfo class
accepts fully qualified path of file.
Length properly of FileInfo instance gives actual size of file.
Note: The size of file on disk varies but actual size remains same.
The size on disk depends upon how disk is formatted; I mean it depends on size of the cluster.
Other Examples
Delete All Files in Directory. Delete all files in directory
Get File Name or Extension of file. Get File Name or Extension C# program
C# File Exists example. File Exists
Currently rated 3.0 by 15 people
- Currently 3/5 Stars.
- 1
- 2
- 3
- 4
- 5