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
Re: Search Sub-Directories Also
From VS IntelliSense with a little change...
VB.NET Code:
Dim files As ReadOnlyCollection(Of String)
Dim wildcards() As String = {".jpg", ".gif", ".bmp", ".png"}
files = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, wildcards)
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
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.
Re: Search Sub-Directories Also
Quote:
Originally Posted by
Negative0
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?
Re: Search Sub-Directories Also
If you really want to search all directories, you can ignore all exceptions...
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.