Solved

DirectoryInfo Error / Getting all directories in a given path

Posted on 2006-07-19
2
223 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now