Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Calculate the size of file

C# Sharp LINQ : Exercise-16 with Solution

Write a program in C# Sharp to calculate size of file using LINQ

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
using System.IO;
class  LinqExercise16
{
    static void Main(string[] args)
    {
        string[] dirfiles = Directory.GetFiles("/home/w3r/abcd/");
				// there are three files in the directory abcd are :
		        // abcd.txt, simple_file.txt and xyz.txt
		
            Console.Write("\nLINQ : Calculate the size of file : "); 
            Console.Write("\n------------------------------------\n");			
		
        var avgFsize = dirfiles.Select(file =>new FileInfo(file).Length).Average();
        avgFsize = Math.Round(avgFsize / 10, 1);
        Console.WriteLine("The Average file size is {0} MB",avgFsize);
        Console.ReadLine();
    }
}

Sample Output:

The Average file size is 3.4 MB  

Flowchart:

Flowchart: LINQ : Calculate the size of file.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Program to Count File Extensions and Group it using LINQ.
Next: Write a program in C# Sharp to Remove Items from List using remove function by passing object.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.