Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
the problem must be solved using Dictionary object not LINQ

What I have tried:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace NoOfFilesByCategory
{

    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter the DirectoryPath");
            string d = Console.ReadLine();
            FilesCounterByCategory files = new FilesCounterByCategory();
            var keyValuePairs = files.NoOfFilesByCategory(d);

            foreach (KeyValuePair<string, string> pair in keyValuePairs)
            {

                Console.WriteLine(pair.Key + ":" + pair.Value);
            }
            /// <summary>
            /// Pass the path and
            /// Display list of file extensions and counts.
            /// </summary>
        }
    }

    public class FilesCounterByCategory
    {
        public Dictionary<string, string> NoOfFilesByCategory(string DirectoryPath)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            string f = Path.GetDirectoryName(DirectoryPath);
            if (Directory.Exists(f))
            {
                DirectoryInfo d = new DirectoryInfo(DirectoryPath);
                FileInfo[] file = d.GetFiles("*.*", SearchOption.AllDirectories);
                for (int i = 0; i < file.Length; i++)
                {
                    int count = 0;

                    if (result.ContainsKey(file[i].Extension))
                    {
                        count++;


                    }
                    else
                    {
                        count = 1;
                    }

                }


            }
            return result;
        }
    }
    /// <summary>
    /// All the class should be written within the namespace 'NoOfFilesByCategory'.
    /// Create Class with Class name 'FilesCounterByCategory'. 
    /// </summary>

    /// <summary>
    /// Create a function with function name 'NoOfFilesByCategory'  to find the number of files based on the category(file extension).
    /// (i.e doc files, xls files, pdf files, etc.,) in the given directory and its sub folders.
    /// <param name="DirectoryPath"></param><type>string</type>
    /// <returns>Dictionary<string, string></returns>
    /// Note: Please dont use Console.WriteLine() and Console.ReadLine() in this method.
    /// </summary>
}
Posted
Updated 7-Dec-22 22:12pm
Comments
PIEBALDconsult 8-Dec-22 11:36am    
Sounds like homework.
And you will likely get incorrect results unless you have 8dot3 names disabled.

1 solution

Quote:
C#
int count = 0;
if (result.ContainsKey(file[i].Extension))
{
    count++;
}
else
{
    count = 1;
}
That code does nothing useful. You declare a local variable and initialize it to 0. If the dictionary contains the extension of the current file, you increment it to 1; otherwise, you set it to 1. You then throw it away.

You also never add anything to the dictionary, so it will always be empty.

What you need to do is read the current count from the dictionary, if it exists; increment it; and then put it back into the dictionary.
C#
if (result.ContainsKey(file[i].Extension))
{
    result[file[i].Extension] = result[file[i].Extension] + 1;
}
else
{
    result[file[i].Extension] = 1;
}
NB: There are more efficient ways to do this with a dictionary; but as this is your homework assignment, I'll assume that either you'll cover them in a later lesson, or you'll research them yourself.
 
Share this answer
 
Comments
sanjay m Dec2022 8-Dec-22 5:15am    
thank you sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900