C# Program to Count File Extensions and Group it using LINQ

«
»

This is a C# Program to count file extensions and group it using linq.

Problem Description

This C# Program Counts File Extensions and Group it using LINQ.

Problem Solution

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.

Program/Source Code

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();
        }
    }
}
Program Explanation

This C# program is used to count file extensions and group it using LINQ. The service reads the files generated in a folder every hour and return a string array containing the file names and shows the count of files grouped by the file extension. Using foreach loop print the file extensions count.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
 
4 File(s) with txt Extension
2 File(s) with pdf Extension
1 File(s) with xml Extension

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.