C# Get File Size

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# Get File Size

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

VB.net Program
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

Tags:

C# Delete All Files In DirectoryAsp.net delete file file on Server C#.net and VB.net codeASP.net File HandlingAsp.net File UploadC# Read File Line By LineGetfilename GetExtensionC# File ExistsRead Text File in (C#.net or VB.net)C#-move files from folders(c#) write to text fileC# File.ReadLines() .net 4.0

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.