C# Program to Calculate the Size of File using LINQ
Posted by Superadmin on August 14 2022 07:00:09

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);
        Console.ReadLine();
    }
}
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