How to get File Size in Asp.Net using C# and VB

← PrevNext →

In Asp.Net, you can use the FileInfo class in both C# and Visual Basic , to get the file size. This class provides the necessary methods and properties to deal with files. Here in this post, I am sharing few examples in C# and VB, explaining how to use the FileInfo class to get the file size.

Example 1

C#

The FileInfo class is a part of the System.IO namespace. So you can use it like this.

using System.IO;

protected void Upload_Files(object sender, EventArgs e)
{
    long objFi = new FileInfo(Server.MapPath("CopyFiles\\image1.jpg")).Length;
    System.Diagnostics.Debug.WriteLine(objFi / (double)1024 + " KB");
}

Alternatively, you can avoid the using statement in the beginning and use the FileInfo class like this.

protected void Upload_Files(object sender, EventArgs e)
{
    long objFi = new System.IO.FileInfo(Server.MapPath("CopyFiles\\image1.jpg")).Length;
    System.Diagnostics.Debug.WriteLine(objFi / (double)1024 + " KB");
}

In the above example, let us assume that a .jpg file named image1.jpg is inside a folder named CopyFiles and this folder is in the root directory.

The Length property returns the size of the file in bytes, which I am converting into Kilo Bytes, by dividing the total bytes by 1024.

That’s how we get the size of a file.

Visual Basic Code

The visual basic code is also simple.

Dim objFi = New System.IO.FileInfo(Server.MapPath("CopyFiles\image1.jpg")).Length
System.Diagnostics.Debug.WriteLine(objFi / 1024 & " KB")

or simply use it like this,

Option Explicit On
Imports System.IO

Dim objFi = New FileInfo(Server.MapPath("CopyFiles\image1.jpg")).Length

Example 2

Here’s another scenario. You might also want to upload multiple files in your Asp.Net application and would like to get size of all the files in a folder before uploading. In such cases, theFileInfo class will return multiple files and you will have iterate or loop through each file to get the size.

C# Code
DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("CopyFiles\\"));
FileInfo[] objFI = objDir.GetFiles();   // Get all the files in the folder.

if (objFI.Length > 0)
{
    int iCnt = 0;       // Just a counter.
    foreach (FileInfo file in objFI)
    {
        System.Diagnostics.Debug.WriteLine(objFI[iCnt].Length / (double)1024 + " KB");
        iCnt += 1;
    }
}

In this example, I am writing the result in the output window (Ctrl + Alt + o). I am using DirectoryInfo class to get information about a given folder. The GetFiles() method of the DirectoryInfo class returns the files in the folder and with it I am initializing the FileInfo class.

Next, I am checking if it has any files (objFI.Length). If yes, I will iterate through each file to get the size.

Visual Basic Code
Dim objDir As New DirectoryInfo(Server.MapPath("CopyFiles\"))
Dim objFI As FileInfo() = objDir.GetFiles()

If objFI.Length > 0 Then
    Dim iCnt As Integer = 0

    For Each file As FileInfo In objFI
        System.Diagnostics.Debug.WriteLine(objFI(iCnt).Length / 1024 & " KB")
        iCnt += 1
    Next
End If

Important

Always check the file type, size etc. at the client side also, before sending it to the server. This is to do a double check of the files before processing it.

← PreviousNext →