This C# Program Counts File Extensions and Group it using LINQ. Here a service reads files generated in a folder every hour and returns a string array containing the file names and showes the count of files grouped by the file extension.
Here is source code of the C# Program to Count File Extensions and Group it using LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/*
* C# Program to Count File Extensions and Group it using LINQ
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication9
{
class Program
{
public static void Main()
{
string[] arr = { "aaa.txt", "bbb.TXT", "xyz.abc.pdf", "aaaa.PDF", "abc.xml", "ccc.txt", "zzz.txt" };
var egrp = arr.Select(file => Path.GetExtension(file).TrimStart('.').ToLower())
.GroupBy(x => x,(ext, extCnt) =>new
{
Extension = ext,
Count = extCnt.Count()
});
foreach (var v in egrp)
Console.WriteLine("{0} File(s) with {1} Extension ",v.Count, v.Extension);
Console.ReadLine();
}
}
}
Here is the output of the C# Program:
4 File(s) with txt Extension
2 File(s) with pdf Extension
1 File(s) with xml Extension
source :http://www.sanfoundry.com/csharp-program-count-file-extensions-linq/
Many answers are just a google away, Have a good day :)