C# Program to Calculate Size of File using LINQ


This is a C# Program to calculate size of file using linq.

Problem Description

This C# Program Calculates Size of File using LINQ.

Problem Solution

Here the size of the folder is found using LINQ functions.

Program/Source Code

Here is source code of the C# Program to Calculate Size of File using LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Calculate Size of File using LINQ
using System;
using System.Linq;
using System.IO;
class Program
    static void Main(string[] args)
        string[] dirfiles = Directory.GetFiles("c:\\sri\\");
        var avg = dirfiles.Select(file =>new FileInfo(file).Length).Average();
        avg = Math.Round(avg / 10, 1);
        Console.WriteLine("The Average file size is {0} MB",avg);
Program Explanation

This C# program used to calculate size of file using LINQ. LINQ (Language Integrated Query) is uniform query syntax used to save and retrieve data from different sources. It is integrated thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.


The size of the folder is found using LINQ functions. Create the files from directory using ‘dirfiles’ variable, the length() function is used to compute the size of the file. Using Round() function compute the division of the value of ‘avg’ variable by 10. Print the size of file.

Runtime Test Cases
The Average file size is 8.8 MB

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn