mithi

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.
  1. /*
  2.  * C# Program to Count File Extensions and Group it using LINQ
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. namespace ConsoleApplication9
  10. {
  11.     class Program
  12.     {
  13.         public static void Main()
  14.         {
  15.             string[] arr = { "aaa.txt", "bbb.TXT", "xyz.abc.pdf", "aaaa.PDF", "abc.xml", "ccc.txt", "zzz.txt" };
  16.             var egrp = arr.Select(file => Path.GetExtension(file).TrimStart('.').ToLower())
  17.                      .GroupBy(x => x,(ext, extCnt) =>new
  18.                                                      {
  19.                                                         Extension = ext,
  20.                                                         Count = extCnt.Count()
  21.                                                       });
  22.  
  23.             foreach (var v in egrp)
  24.                 Console.WriteLine("{0} File(s) with {1} Extension ",v.Count, v.Extension);
  25.             Console.ReadLine();
  26.         }
  27.     }
  28. }
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

0 comments:

Post a Comment

 
Top
Mithilesh Joshi