0% found this document useful (0 votes)
74 views

C# Program To Count File Extensions and Group It Using Linq

This C# program uses LINQ to count file extensions and group them by extension in a given folder. It reads file names from a string array, extracts the extension for each file using Path.GetExtension, trims the leading period and converts to lowercase. It groups the extensions and counts the number of files for each extension. The output displays the count of files for each extension.

Uploaded by

Rajthilak24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

C# Program To Count File Extensions and Group It Using Linq

This C# program uses LINQ to count file extensions and group them by extension in a given folder. It reads file names from a string array, extracts the extension for each file using Path.GetExtension, trims the leading period and converts to lowercase. It groups the extensions and counts the number of files for each extension. The output displays the count of files for each extension.

Uploaded by

Rajthilak24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

11.

C# Program to Count File Extensions and Group it using


LINQ

This C# Program Counts File Extensions and Group it using LINQ. Here a service reads
filesgenerated 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.

{
string[] arr = { "aaa.txt", "bbb.TXT", "xyz.abc.pdf",

15.

"aaaa.PDF", "abc.xml", "ccc.txt", "zzz.txt" };


var egrp = arr.Select(file =>

16.

Path.GetExtension(file).TrimStart('.').ToLower())
.GroupBy(x => x,(ext, extCnt) =>new

17.

18.
19.

Extension
= ext,
Count =

20.
extCnt.Count()

});

21.
22.
foreach (var v in egrp)

23.

Console.WriteLine("{0} File(s) with {1} Extension

24.
",v.Count, v.Extension);

Console.ReadLine();

25.
26.

27.
28.

You might also like