Solved

DirectoryInfo Error / Getting all directories in a given path

Posted on 2006-07-19
2
232 Views
Last Modified: 2010-04-23
I'm using the following code which appears as if it should get all the directories on my hard drive (or any given path).  The problem is, even as an admin of my machine there are protected directories that I apparantly don't have access too and it throws an access exception on the for each line (The specific error is:  Access to the path 'c:\WINDOWS\Cannot delete for whatever reason\8fae693e482ad5d9c6a7b4e423dcd452\sp1' is denied.).  Is there an obvious solution I'm missing without writing more code to recurse manually?

I'm less concerned with the fact that I don't have access to certain directories, that will happen in how I'm going to use this anyway.  I'd prefer it just skipped those directories and moved on (the GetDirectories halts though on this exception).

        Dim rootDi As New DirectoryInfo("c:\")

        For Each di As DirectoryInfo In rootDi.GetDirectories("*", SearchOption.AllDirectories)
            Label1.Text = di.FullName
            Application.DoEvents()
        Next
0
Comment
Question by:IUFITS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
LordWabbit earned 500 total points
ID: 17144943
Nope some folders are off limits, your best option would be to do some manual recursion like

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetDirectories("C:\")
    End Sub

    Private Sub GetDirectories(ByVal Path As String)
        Try
            Dim arrDir() As String = System.IO.Directory.GetDirectories(Path)
            For Each eleDir As String In arrDir
                ListBox1.Items.Add(eleDir)
                Application.DoEvents()
                GetDirectories(eleDir)
            Next
        Catch ex As Exception

        End Try
    End Sub
0
 
LVL 3

Author Comment

by:IUFITS
ID: 17146825
That appears to do the trick since it errors out on just directories it can't read.  Thanks for a quick and correct response.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question