Answer to Question #180525 in C# for Miley

Question #180525

Get all the files from a given directory and perform the following actions

1.       Return the number of text files in the directory (*.txt).

2.       Return the number of files per extension type.

3.       Return the top 5 largest files, along with their file size (use anonymous types).

4.       Return the file with maximum length


1
Expert's answer
2021-04-17T01:29:49-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Q180525
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {


                //Get all the files from a given directory and perform the following actions
                string directoryPath = @"F:\test";
                Console.Write("Enter the directory (example: F:\\test) ");
                directoryPath=Console.ReadLine();
                if (Directory.Exists(directoryPath))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
                    //1.       Return the number of text files in the directory (*.txt).
                    int numberTextFiles = directoryInfo.GetFiles().ToList().Where(file => file.FullName.Contains(".txt")).Count();
                    Console.WriteLine("\nThe number of text files in the directory (*.txt): {0}", numberTextFiles);
                    //2.       Return the number of files per extension type.
                    Console.WriteLine("\nThe number of files per extension type:");
                    var extensionTypeCounters = directoryInfo.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly)
                            .GroupBy(file => file.Extension)
                            .Select(g => new { Extension = g.Key, Count = g.Count() })
                            .ToList();


                    foreach (var extensionTypeCounter in extensionTypeCounters)
                    {
                        Console.WriteLine("The number of files with extension \"{0}\" is: {1}", extensionTypeCounter.Extension, extensionTypeCounter.Count);
                    }
                    //3.       Return the top 5 largest files, along with their file size (use anonymous types).
                    Console.WriteLine("\nThe top 5 largest files, along with their file size:");
                    var topLargestFiles = directoryInfo.GetFiles().OrderByDescending(file => file.Length).Take(5).ToList();
                    foreach (FileInfo fileInfo in topLargestFiles)
                    {
                        Console.WriteLine("The file \"{0}\" has size {1} MB", fileInfo.Name, fileInfo.Length / 1024f / 1024f);
                    }
                    //4.       Return the file with maximum length
                    if (topLargestFiles.Count != 0) {
                        Console.WriteLine("\nThe file \"{0}\" has maximum length in the directory {1} MB", ((FileInfo)topLargestFiles[0]).Name, ((FileInfo)topLargestFiles[0]).Length / 1024f / 1024f);
                    }


                }
                else
                {
                    Console.WriteLine("The directory does not exist.");
                }


            }
            catch (Exception ex)
            {


            }


            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS