Solved

DirectoryInfo Error / Getting all directories in a given path

Posted on 2006-07-19
2
231 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
get row value in vb.net 4 18
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 44
VB.Net. Reading xml value 6 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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