|
-
Jul 18th, 2009, 06:45 PM
#1
Thread Starter
Lively Member
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
-
Jul 18th, 2009, 08:12 PM
#2
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)
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 18th, 2009, 08:25 PM
#3
Thread Starter
Lively Member
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
-
Jul 18th, 2009, 08:52 PM
#4
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.
-
Jul 18th, 2009, 09:42 PM
#5
Thread Starter
Lively Member
Re: Search Sub-Directories Also
 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?
-
Jul 19th, 2009, 07:06 AM
#6
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
-
Jul 19th, 2009, 10:03 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|