Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am a bit stuck as to why my label will not count all the directories and sub directories in C:\

I do not wish to count the files, I only wish to get a quick count for all existing Folders in which ever drive I choose.

Here is what i have so far:

Try
                Dim Root As New DirectoryInfo("SearchPath eg. C:\")
                Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*", SearchOption.AllDirectories)
                Dim count As Integer = Dirs.Length
                For Each DirectoryName In Dirs
                    Label12.Text = count
                Next
            Catch ex As UnauthorizedAccessException
            End Try



If I try to load the program with this I get a path access violation saying documents and settings is protected.

A quick example would be greatly appricated or an explaination of why this does not work would be just as equal.

thank you in advance!!
Posted

1 solution

Use Directory.GetDirectories("c:\") instead.

Also DirectoryInfo takes a valid path, so it should be DirectoryInfo("C:\").
 
Share this answer
 
Comments
Dale 2012 10-Aug-12 4:43am    
Ok this is a bit confusing your reply is is saying that I should change

Dim Root As New DirectoryInfo("SearchPath eg. C:\")

to
dim root = directory.getdirectories("C:\")

but then you go to say directoryinfo takes a valid path so it should be directoryinfo ("C:\") ??????

I have got the code to all ready return all folder count for the first level of C:\ but it does not go any further....

I need this to go recrusivly into all area's of the C:\ to return a full folder count to my label...

thank you for your response can you plz add a bit to this so that it works??

cheers!
Mehdi Gholam 10-Aug-12 4:53am    
Those are 2 different options.

The code you posted is wrong it should be DirectoryInfo("C:\"), Directory.GetDirectories is another way.

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