0

So, I am trying to make a File-Search-Program, which should also search through all sub-folders of the given folder. The problem is, that if I want to search on C:\ for example, I get an UnauthorizedAccessException. I know that I could make a "Try"-Statement, but that doesn't work either because then the FileInfo stays Nothing.

Here's my code:

Sub SubfolderSearch()

    Dim diar2 As IO.FileInfo()
    Dim di2 As New IO.DirectoryInfo(Path)

    diar2 = di2.GetFiles("*", IO.SearchOption.AllDirectories) 'The Error occurs in this line

    Dim dra As IO.FileInfo

        For Each dra In diar2

            Dim FileName As String = dra.Name
            Dim FilePath As String = dra.FullName
            Dim FileSize As Integer = dra.Length / 1000

            'Search Procedure'

        Next

        SubfolderThread.Abort()
Vivek
  • 11,938
  • 19
  • 92
  • 127
OneByte_
  • 81
  • 11
  • You're approaching this incorrectly with the `Try..Catch`. You'll be able to get around that by attempting an operation on `FileName` (such as `If (Not FileName.ToString() == "") Then`. – AStopher Dec 13 '15 at 16:11
  • I think you didn't exactly get it. I'm trying to get the FileInfo of all the files I have access to, in order to be able to read the FileName and all that stuff. – OneByte_ Dec 13 '15 at 16:14
  • 1
    If you have access to these files, why are you getting an UnauthorizedAccessException? Is Windows lying to you? Sorry but we cannot fix this either. – varocarbas Dec 13 '15 at 16:16
  • No, I mean I have access to SOME files, but not all of them. The FileInfo.GetFiles although tries to get ALL files, even if I don't have access to them. And if it tries to get the FileInfo of an unaccessable file I get the error, like for example I have access to the "Windows" Folder, but not to "Documents and Settings". – OneByte_ Dec 13 '15 at 16:20
  • Did you try what I said? – AStopher Dec 13 '15 at 16:20
  • Well, I can't get the FileName if I don't have the FileInfo, so what you said was not possible. :( – OneByte_ Dec 13 '15 at 16:21
  • 2
    Yes, that's why I said to **use Try..Catch**. If it fails, it'll break out & try the next file. – AStopher Dec 13 '15 at 16:23
  • Am I not understanding something? Where should I use the Try..Catch? Sorry if I'm just being stupid now. >_ – OneByte_ Dec 13 '15 at 16:25
  • 1
    The question still continues without making any sense and @cybermonkey approach seems what you are looking for. That is: GetFiles only gets a list of names (= no error is triggered) then you can do whatever you want with this list: either skip the names which you know that are wrong or rely on try catch which will take care of the wrong ones automatically (i.e., will allow the loop to continue despite of the errors). But the underlying idea is exactly the same: asking what you can do to avoid an UnauthorizedAccessException seems extremely offtopic (= just avoid whatever provokes it). – varocarbas Dec 13 '15 at 16:32
  • @OneByte_ Provided an answer. – AStopher Dec 13 '15 at 16:34
  • The only problem is, that the UnauthorizedAccessException occurs at the GetFiles statement, so there is an error triggered there. – OneByte_ Dec 13 '15 at 16:36
  • @OneByte_ You didn't make this clear. Please update your question with *exactly* where the error occurs. – AStopher Dec 13 '15 at 16:37
  • 2
    Duplicate of [Ignore folders/files when Directory.GetFiles() is denied access](http://stackoverflow.com/questions/172544/ignore-folders-files-when-directory-getfiles-is-denied-access) (essentially asks the exact same thing, doesn't matter that the languages are different as they are both .NET). – AStopher Dec 13 '15 at 16:41
  • Okay, sorry, I edited the question now. – OneByte_ Dec 13 '15 at 16:41
  • I realised now about what is the problem here. You should be using `System.IO.Directory.GetFiles` (I intuitively thought that this was the case), it just gets the file names and doesn't provoke any access problem. The version you are using (the one of `DirectoryInfo`, with the same name but different, is much more aggressive and does have to access the files). Using the aforementioned alternative would fix your problem (unless you don't have access to the root directory; in that case, there is no solution). – varocarbas Dec 13 '15 at 17:43

1 Answers1

-1

You need to use Directory.EnumerateFiles() to read file by file so that you can ignore access denied exceptions. If you use the GetFiles() function, you will end up returning the whole array and will be unable to catch exceptions and continue if one is not accessible.

MSDN says:

The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

So use it like this:

For Each dra In di2.EnumerateFiles("*", IO.SearchOption.AllDirectories)
    Dim FileName As String = dra.Name
    Dim FilePath As String = dra.FullName
    Dim FileSize As Integer = dra.Length / 1000
    'Search Procedure'
    'Put a try-catch block here.
Next
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52