c sharp

How to Get File Size in C# Applications

If you are building an application that works with files, you are most definitely going to need the functionality to determine the file size. This can help you implement the features such as file size limiting. It also helps you to choose whether you wish to load the file into the memory or stream it to avoid a resource waste.

In this tutorial, we will cover the various methods and techniques that you can implement in your C# code to determine the size of a file.

C# FileInfo Class

One of the most useful class from the System.IO namespace is the “FileInfo” class. This class provides us with an extensive list of properties and methods to create, delete, move, and open the files. It also provides with methods for fetching the file metadata and more.

Method 1: Using the FileInfo Class

As you can guess, we can use this class to gather an information about the size of a given file using the “Length” property.

Consider the following example demonstration:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\sample\linuxhint\sample.zip";
        FileInfo info = new FileInfo(path);

        long fsBytes = info.Length;
        Console.WriteLine("Bytes: " + fsBytes);
    }
}

In the given example, we start by importing the System.IO namespace. This gives us an access to the “FileInfo” class and the “Length” property.

We then specify the path to the file whose size we wish to retrieve.

Once we create an instance of the “FileInfo” class, we pass the file path as the parameter and access the “Length” property to get the size of the file.

This should return the size of the file in bytes which we can print in the console. The resulting output is as follows:

Bytes: 22

This is correct as we can confirm from the File Explorer file info window.

Method 2: Using the File Class

We can also take advantage of the “File” class to fetch the size of a given file. Unlike the “FileInfo” class, this class does not provide a direct method of retrieving the file size. Hence, we need to combine the functionality of multiple methods.

Take a look at the following example code:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\sample\linuxhint\sample.zip";
        byte[] rdBytes = File.ReadAllBytes(path);
        long fsBytes = rdBytes.Length;
        Console.WriteLine("Size: " + fsBytes);
    }
}

The method works by reading the bytes from the specified file using the ReadAllBytes() method. We then obtain the length of the byte array which is correspondent to the file size in bytes.

NOTE: This technique does an overhead to the process as it involves reading the file which is an unnecessary step. However, it is good to know about it.

Method 3: Using the FileStream Class

The last class that we can use to fetch the size of file is the “FileStream” class. Similar to the “FileInfo” class, this class has access to the “Length” property which returns the size of a file in bytes.

Example usage:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\sample\linuxhint\sample.zip";
        using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            long fsBytes = fileStream.Length;
            Console.WriteLine("Size: " + fsBytes);
        }
    }
}

This should return the size of the specified file in bytes.

Conclusion

In this tutorial, we explored the main methods that we can use to fetch the size of a file in C# applications.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list