473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Determine Number and Count of Unique Extensions

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(myCurrentFi le.Extension.ToLower());

if (myIndexOfValue == -1)
{
this._ArrayListOfExtensions.Add(myCurrentFile.Exte nsion.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
Oct 20 '08 #1
3 1341
Edwin <Ed***@discussions.microsoft.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 20 '08 #2
Thank you Jon for your reply,

I am using Visual Studio 2008 Professional with .Net 3.5

I appreciate your assistance.

Edwin

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Edwin <Ed***@discussions.microsoft.comwrote:
>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...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 20 '08 #3
Edwin <ed*********@msn.comwrote:
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>.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mr_burns | last post by:
hi, i am wanting to count the number of unique rows under a certain column. i want to count how many times a page has been accessed. how i plan to do this is every time a page is accessed, a...
1
by: Bryan Zash | last post by:
When querying a bit field, I am encountering a problem with MS SQL Server returning a larger number of records for a table than the actual number of records that exist within that table. For...
0
by: johkar | last post by:
My XML and XSL is below. Also below is a textual representation of what I want to get out of the XML with XSL. For each Extension node in XML, I am only concerned with those nodes with...
20
by: William Stacey [MVP] | last post by:
int list = {1,2,3,4,5,6}; Function to randomize the list? Cheers! -- William Stacey, MVP
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
6
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi, I'd appreciae any advice on how to do this in VB 2005. I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and etc... I like to determine how many unqiue "States" are...
8
by: Anil Gupte | last post by:
I had someone write a random number generator in C# (I am more of a VB programmer) and they came up with the following: public string GetRand(int count) { string number = ""; for (int i=0;...
1
by: dez5000 | last post by:
I'm trying to get a report by location that would list the number of visits to the location for the month but also count the number of unique visitors to that location. I have a table of data with...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.