Determine Number and Count of Unique Extensions

E

Edwin

What I am trying to do is come up with a way to determine how many unique
extensions there are within a directory and its sub directories. Within the
loop that recurses root folder, I am attempting to use a SortedList. Below
is the code:

private System.Collections.SortedList _ArrayListOfExtensions = new
SortedList(new CaseInsensitiveComparer());

System.Int32 myIndexOfValue = -1;
myIndexOfValue =
this._ArrayListOfExtensions.IndexOfKey(myCurrentFile.Extension.ToLower());

if (myIndexOfValue == -1)
{
this._ArrayListOfExtensions.Add(myCurrentFile.Extension.ToLower(), 1);
}

The Exception error that I am getting is:
"Unable to cast object of type 'System.Collections.DictionaryEntry' to type
'System.String'."

Any help would be appreciated as to how to correct this error. Also, if you
know of a better way (I have tried several) to accomplish the above, I am
open to your suggestions.

The results should be similar to:

15 .docx
17 .doc
14 .txt
7 .exe
etc...

Thanks!
Edwin
 
J

Jon Skeet [C# MVP]

Edwin said:
What I am trying to do is come up with a way to determine how many unique
extensions there are within a directory and its sub directories. Within the
loop that recurses root folder, I am attempting to use a SortedList. Below
is the code:

private System.Collections.SortedList _ArrayListOfExtensions = new
SortedList(new CaseInsensitiveComparer());

SortedList isn't an ArrayList - it's a dictionary, basically.

Are you using .NET 3.5? LINQ would make this pretty much trivial...
 
E

Edwin

Thank you Jon for your reply,

I am using Visual Studio 2008 Professional with .Net 3.5

I appreciate your assistance.

Edwin
 
J

Jon Skeet [C# MVP]

Edwin said:
Thank you Jon for your reply,

I am using Visual Studio 2008 Professional with .Net 3.5

I appreciate your assistance.

In which case I suggest you look at the overloads of Enumerable.GroupBy
which take an IEqualityComparer<TKey>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top