My first suggestion would be to not use "On Error Resume Next" in .Net, this is retained for backwards compatibility but there is little if any reason to continue using it because vb.net features structured exception handling.
It works like:
CODE
Try
'Your code
'Goes here
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
Also, this code seems like it would be slow, and here especially:
CODE
For Each ext As String In extensions
Dim aryFi As IO.FileInfo() = dirInfo.GetFiles(ext)
Dim fi As IO.FileInfo
For Each fi In aryFi
tempstring = fi.Name
AccessControl2Copy()
Next
Next
I don't see why you have to re-iterate through the same directory to get files of each type. You should be able to simply run through it once and check each file extension for the ones that you are looking for. But really, all in all, yes there is a better way to do this.
I personally would use two methods, one to do the recursion through directories and another to just get the files contained within. Like in this little example:
CODE
Dim strExts() As String = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff"}
Public Sub GetDirs(ByVal path As String, Optional ByVal getSubDirs As Boolean = True)
Try
For Each subDir As String In Directory.GetDirectories(path)
GetFiles(subDir)
If getSubDirs Then
GetDirs(subDir)
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub
Public Sub GetFiles(ByVal path As String)
Try
For Each strFile As String In Directory.GetFiles(path)
Dim fInfo As New FileInfo(strFile)
'If fInfo.Extension = ".png" Then
If strExts.Contains(fInfo.Extension) Then
lstFiles.Items.Add(fInfo.FullName)
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Sub
Just have a listbox named "lstFiles" on the form and a button to start it like so:
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetFiles("C:\Users") 'Get the files in this path
GetDirs("C:\Users") 'Start recursion
End Sub
See if you can learn from that and adapt it to fit your needs.
By the way, make sure to put this line at the very top of your source file so you don't have to keep typing "[System.]IO.-":
CODE
Imports System.IO 'This has to be there
Public Class Form1
'-----------------------------------
End Class
This post has been edited by LoveIsNull: 2 Jul, 2009 - 04:09 PM