GetFiles exception

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi everyone,

I'm trying to find the fastest way to get all the files from the local c:
drive. I have even considered the api calls findfirst/findnext, but read
that in VB.net it's best to use the GetFiles function.

I'm using:
Dim files As String() = System.IO.Directory.GetFiles("c:\", "*.*",
IO.SearchOption.AllDirectories)

and get UnAuthorizeException was unhandled: "Access to the path 'c:\System
Volume Information' is denied."

Is there any way I can just ignore the exception and have the program
continue where it left off?


Thanks,

Chris
 
Put a Try/Catch around that line, then do nothing in the catch. In the code
following the try/catch, make sure you are not relying on GetFiles
succeeding.
 
Hi Marina,

The problem with this solution is that I need the GetFiles to pickup where
it left off.

The end result should be an array that contains every file on my c: drive.

What else can I do?

Thanks,

Chris
 
That's not the same as ignoring the error thrown and continue with your
code. Please be clear in what you want to do.

It sounds like you just need this code to work instead of throwing the
exception. In which case, the user your executable is running under needs
sufficient permissions to get all the files on the drive. Right now this
user does not have these permissions.
 
The followinig code works, but I'm looking for something even faster.

Public Function RecursiveSearch(ByRef array As ArrayList, _
ByVal strDir As String, ByVal bwWorker As BackgroundWorker, _
ByVal e As DoWorkEventArgs) As Boolean

Dim dirInfo As New IO.DirectoryInfo(strDir)

' Try to get the files for this directory
Dim pFileInfo() As IO.FileInfo
Try
pFileInfo = dirInfo.GetFiles()
Catch ex As UnauthorizedAccessException
Return False
Exit Function
End Try

' Add the file infos to the array
array.AddRange(pFileInfo)

'report progress, raises progress changed event
bwWorker.ReportProgress(array.Count)

' Try to get the subdirectories of this one
Dim pdirInfo() As IO.DirectoryInfo
Try
pdirInfo = dirInfo.GetDirectories()
Catch ex As UnauthorizedAccessException
Return False
Exit Function
End Try

' Iterate through each directory and recurse
Dim dirIter As IO.DirectoryInfo
For Each dirIter In pdirInfo
If bwWorker.CancellationPending Then
e.Cancel = True
Else
RecursiveSearch(array, dirIter.FullName, bwWorker, e)
End If
Next dirIter
Return True
End Function
 
Perhaps a crazy idea... How about getting the result of
dir *.* /b/s
and appling a split function to the result ?

-tom

Marina Levit [MVP] ha scritto:
 
This will work too....

Private FileList As String = ""

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

GetFiles("C:\")
FileList = FileList.TrimEnd(",")
ListBox1.DataSource = FileList.Split(",")

End Sub

Private Sub GetFiles(ByVal Path As String)
Dim SubDirs() As IO.DirectoryInfo
Dim SubDir As IO.DirectoryInfo
Dim Files() As IO.FileInfo
Dim File As IO.FileInfo

Dim CurDir As New IO.DirectoryInfo(Path)

Files = CurDir.GetFiles

If Not Files Is Nothing Then
For Each File In Files
FileList = FileList & File.Name & ","
Next
End If

SubDirs = CurDir.GetDirectories

If Not SubDirs Is Nothing Then
For Each SubDir In SubDirs
Try
GetFiles(SubDir.FullName)
Catch ex As Exception
'Do Nothing
End Try
Next
End If

End Sub
 
Chris said:
Hi everyone,

I'm trying to find the fastest way to get all the files from the local c:
drive. I have even considered the api calls findfirst/findnext, but read
that in VB.net it's best to use the GetFiles function.

I'm using:
Dim files As String() = System.IO.Directory.GetFiles("c:\", "*.*",
IO.SearchOption.AllDirectories)

and get UnAuthorizeException was unhandled: "Access to the path 'c:\System
Volume Information' is denied."

Is there any way I can just ignore the exception and have the program
continue where it left off?

I haven't reied this yet on a boot device, but you can give it a try:

http://groups.google.com/group/micr...f110c6e2eece5?q=zacks&rnum=2#c2ef110c6e2eece5
 
Back
Top