C# Program to Calculate the 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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.