Results 1 to 7 of 7

Thread: Search Sub-Directories Also

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Search Sub-Directories Also

    Right now this code just searches 1 directory, how can i get it to search the original directory and all the sub-directories

    Code:
    Imports System.IO
    
    Public Class Form1
        Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("C:\Pictures")
    
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
        Dim fi As IO.FileInfo
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            For Each fi In aryFi
                If fi.Extension = ".jpg" Or fi.Extension = ".gif" Or fi.Extension = ".bmp" Or fi.Extension = ".png" Or fi.Extension = ".PNG" Or fi.Extension = ".JPG" Then ''etc etc etc
                    ListBox1.Items.Add(fi.Name)
                End If
            Next
    
        End Sub
    End Class

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Search Sub-Directories Also

    From VS IntelliSense with a little change...

    VB.NET Code:
    1. Dim files As ReadOnlyCollection(Of String)
    2.         Dim wildcards() As String = {".jpg", ".gif", ".bmp", ".png"}
    3.         files = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, wildcards)

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: Search Sub-Directories Also

    its not recognizing ReadOnlyCollection(Of String), saying it's not declared, i'm using .netframework 2.0 for this project

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Search Sub-Directories Also

    It is part of the System.Collections.ObjectModel namespace, so you may need an import on that.

    Also, be careful with the SearchAllSubDirectories option on machines that are locked down, especially Vista machines. It will throw an exception and stop processing if it runs into files that it does not have access to.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    84

    Re: Search Sub-Directories Also

    Quote Originally Posted by Negative0 View Post
    It is part of the System.Collections.ObjectModel namespace, so you may need an import on that.

    Also, be careful with the SearchAllSubDirectories option on machines that are locked down, especially Vista machines. It will throw an exception and stop processing if it runs into files that it does not have access to.
    OK I got it working, but it is stopping at stuff like documents & settings, is there any way to bypass these restrictions?

  6. #6
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Search Sub-Directories Also

    If you really want to search all directories, you can ignore all exceptions...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Search Sub-Directories Also

    That won't work if you're using the recursive function shown above, because when it errors out it just won't return anything at all. You'll have to do a manual function to recurse through subdirectories and handle errors.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width