w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: LINQ : 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?



New Content: Composer: Dependency manager for PHP, R Programming