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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework