none
Directory.GetFileSystemEntries - "access denied" - anyway around this?

    Question

  • Hi All,

    I have tried a recursive search and no joy. I have tried multiple searches as ADMIN and no joy. I can't get past this no matter how much googling I do.

    Is there a way to search for all files in all sub-directories staring at Root?
    If I start my search at a sub-directory then it loads my array fine.
    If I start at the ROOT of any Disk (C:\ or E:\ or any) then I get 'access denied' when it hits a particular file. On the C:\ drive ROOT, it craps out on the first Directory Read and when I display the Directory it is not a Directory I can see in explorer. It looks like it's a Hidden Directory.

    Is there a way to ignore any errors thrown and bypass it to read the next directory and file? What I would like to do is just load any files in directories that the user has access too.

    -NJ

          Dim allDirectories As String() = Nothing
    
            Try
                allDirectories = Directory.GetFileSystemEntries(FoldertoProcess, "*", IO.SearchOption.AllDirectories)
            Catch ex As Exception
                worker.ReportProgress(ProcessRow, "Error")
                Exit Sub
            End Try

    Monday, March 20, 2017 7:32 PM

Answers

  • Hi All,

    I have tried a recursive search and no joy. I have tried multiple searches as ADMIN and no joy. I can't get past this no matter how much googling I do.

    Is there a way to search for all files in all sub-directories staring at Root?
    If I start my search at a sub-directory then it loads my array fine.
    If I start at the ROOT of any Disk (C:\ or E:\ or any) then I get 'access denied' when it hits a particular file. On the C:\ drive ROOT, it craps out on the first Directory Read and when I display the Directory it is not a Directory I can see in explorer. It looks like it's a Hidden Directory.

    Is there a way to ignore any errors thrown and bypass it to read the next directory and file? What I would like to do is just load any files in directories that the user has access too.

    -NJ

          Dim allDirectories As String() = Nothing
    
            Try
                allDirectories = Directory.GetFileSystemEntries(FoldertoProcess, "*", IO.SearchOption.AllDirectories)
            Catch ex As Exception
                worker.ReportProgress(ProcessRow, "Error")
                Exit Sub
            End Try

    The following will get you all "safe" directories, including from an entire drive:

    Private Function GenerateSafeFolderList(ByVal folder As String) _ As List(Of String) ' ------------------------------------------- ' Based On A Function By John Wein As Posted: ' ' http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d6e64558-395b-4b48-8b64-0f5a7e3a7623 ' ' Thanks John! ' ------------------------------------------- Dim retVal As New List(Of String) Dim Dirs As New Stack(Of String) Dirs.Push(folder) While Dirs.Count > 0 If _userCancel Then Exit While Else Dim Dir As String = Dirs.Pop Try For Each D As String In System.IO.Directory.GetDirectories(Dir) ' Do not include any that are either system or hidden Dim dirInfo As New System.IO.DirectoryInfo(D) If (((dirInfo.Attributes And System.IO.FileAttributes.Hidden) = 0) AndAlso _ ((dirInfo.Attributes And System.IO.FileAttributes.System) = 0)) Then If Not retVal.Contains(D) Then retVal.Add(D) End If End If Dirs.Push(D) Next Catch ex As Exception If retVal.Contains(Dir) Then Dim indexToRemove As Integer = 0 For i As Integer = 0 To retVal.Count - 1 If retVal(i) = Dir Then indexToRemove = i Exit For End If Next retVal.RemoveAt(indexToRemove) End If Continue While End Try End If End While Return retVal End Function


    It can take a bit to do, so do be aware of that.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by NJDevils28 Monday, March 20, 2017 8:53 PM
    Monday, March 20, 2017 8:15 PM

All replies

  • Hi All,

    I have tried a recursive search and no joy. I have tried multiple searches as ADMIN and no joy. I can't get past this no matter how much googling I do.

    Is there a way to search for all files in all sub-directories staring at Root?
    If I start my search at a sub-directory then it loads my array fine.
    If I start at the ROOT of any Disk (C:\ or E:\ or any) then I get 'access denied' when it hits a particular file. On the C:\ drive ROOT, it craps out on the first Directory Read and when I display the Directory it is not a Directory I can see in explorer. It looks like it's a Hidden Directory.

    Is there a way to ignore any errors thrown and bypass it to read the next directory and file? What I would like to do is just load any files in directories that the user has access too.

    -NJ

          Dim allDirectories As String() = Nothing
    
            Try
                allDirectories = Directory.GetFileSystemEntries(FoldertoProcess, "*", IO.SearchOption.AllDirectories)
            Catch ex As Exception
                worker.ReportProgress(ProcessRow, "Error")
                Exit Sub
            End Try

    The following will get you all "safe" directories, including from an entire drive:

    Private Function GenerateSafeFolderList(ByVal folder As String) _ As List(Of String) ' ------------------------------------------- ' Based On A Function By John Wein As Posted: ' ' http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d6e64558-395b-4b48-8b64-0f5a7e3a7623 ' ' Thanks John! ' ------------------------------------------- Dim retVal As New List(Of String) Dim Dirs As New Stack(Of String) Dirs.Push(folder) While Dirs.Count > 0 If _userCancel Then Exit While Else Dim Dir As String = Dirs.Pop Try For Each D As String In System.IO.Directory.GetDirectories(Dir) ' Do not include any that are either system or hidden Dim dirInfo As New System.IO.DirectoryInfo(D) If (((dirInfo.Attributes And System.IO.FileAttributes.Hidden) = 0) AndAlso _ ((dirInfo.Attributes And System.IO.FileAttributes.System) = 0)) Then If Not retVal.Contains(D) Then retVal.Add(D) End If End If Dirs.Push(D) Next Catch ex As Exception If retVal.Contains(Dir) Then Dim indexToRemove As Integer = 0 For i As Integer = 0 To retVal.Count - 1 If retVal(i) = Dir Then indexToRemove = i Exit For End If Next retVal.RemoveAt(indexToRemove) End If Continue While End Try End If End While Return retVal End Function


    It can take a bit to do, so do be aware of that.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by NJDevils28 Monday, March 20, 2017 8:53 PM
    Monday, March 20, 2017 8:15 PM
  • Hi All,

    I have tried a recursive search and no joy. I have tried multiple searches as ADMIN and no joy. I can't get past this no matter how much googling I do.

    Is there a way to search for all files in all sub-directories staring at Root?
    If I start my search at a sub-directory then it loads my array fine.
    If I start at the ROOT of any Disk (C:\ or E:\ or any) then I get 'access denied' when it hits a particular file. On the C:\ drive ROOT, it craps out on the first Directory Read and when I display the Directory it is not a Directory I can see in explorer. It looks like it's a Hidden Directory.

    Is there a way to ignore any errors thrown and bypass it to read the next directory and file? What I would like to do is just load any files in directories that the user has access too.

    -NJ

          Dim allDirectories As String() = Nothing
    
            Try
                allDirectories = Directory.GetFileSystemEntries(FoldertoProcess, "*", IO.SearchOption.AllDirectories)
            Catch ex As Exception
                worker.ReportProgress(ProcessRow, "Error")
                Exit Sub
            End Try

    The following will get you all "safe" directories, including from an entire drive:

    Private Function GenerateSafeFolderList(ByVal folder As String) _ As List(Of String) ' ------------------------------------------- ' Based On A Function By John Wein As Posted: ' ' http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d6e64558-395b-4b48-8b64-0f5a7e3a7623 ' ' Thanks John! ' ------------------------------------------- Dim retVal As New List(Of String) Dim Dirs As New Stack(Of String) Dirs.Push(folder) While Dirs.Count > 0 If _userCancel Then Exit While Else Dim Dir As String = Dirs.Pop Try For Each D As String In System.IO.Directory.GetDirectories(Dir) ' Do not include any that are either system or hidden Dim dirInfo As New System.IO.DirectoryInfo(D) If (((dirInfo.Attributes And System.IO.FileAttributes.Hidden) = 0) AndAlso _ ((dirInfo.Attributes And System.IO.FileAttributes.System) = 0)) Then If Not retVal.Contains(D) Then retVal.Add(D) End If End If Dirs.Push(D) Next Catch ex As Exception If retVal.Contains(Dir) Then Dim indexToRemove As Integer = 0 For i As Integer = 0 To retVal.Count - 1 If retVal(i) = Dir Then indexToRemove = i Exit For End If Next retVal.RemoveAt(indexToRemove) End If Continue While End Try End If End While Return retVal End Function


    It can take a bit to do, so do be aware of that.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    That worked - Thanks!!!

    • Marked as answer by NJDevils28 Monday, March 20, 2017 8:26 PM
    • Unmarked as answer by NJDevils28 Monday, March 20, 2017 8:53 PM
    Monday, March 20, 2017 8:26 PM
  • That worked - Thanks!!!
    Great but why did you mark yourself as the answerer?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, March 20, 2017 8:29 PM
  • That worked - Thanks!!!

    Great but why did you mark yourself as the answerer?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Oops!

    Fixed!

    Monday, March 20, 2017 8:54 PM
  • That worked - Thanks!!!

    Great but why did you mark yourself as the answerer?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Oops!

    Fixed!


    Thanks. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, March 20, 2017 9:08 PM