Unless you're the Admin, you won't get access. So if you want your code to avoid folders/files that it cannot access, you'll need to handle that in code. The DirectoryInfo command will fail, so you will need to implement your own recursion.
Here's an example;
If you call this sub, it will generate a list of folders that were accessible (FolderList) and will also generate a list of folders (FolderFailedAccessList) that could not be traversed.Code:Dim FolderFailedAccessList As List(Of String) Dim FolderList As List(Of String) Private Sub RecurseFolders(ByVal path As String) ' This sub recurses down a folder tree (path) and finds all accessible folder paths ' It also updates a list of folders where an error was incurred getting access Try Dim subfolders() As String = IO.Directory.GetDirectories(path, "*.*", IO.SearchOption.TopDirectoryOnly) For Each folder As String In subfolders Try FolderList.Add(folder) RecurseFolders(folder) Catch ex As Exception FolderFailedAccessList.Add(path) End Try Next Catch ex As Exception FolderFailedAccessList.Add(path) End Try End Sub Private Sub GatherFolders(ByVal path As String) ' This sub gathers all the folders in the path and updates two lists ' FolderList = A list of all accessible folder paths ' FolderFailedAccessList = A list that could not be inspected due to access problems If System.IO.Directory.Exists(path) Then Dim searchpath As String If System.IO.Path.GetExtension(path) = ".lnk" Then searchpath = ShellMethods.GetShortcutPath(path) Else searchpath = path End If FolderFailedList = New List(Of String) FolderList = New List(Of String) FolderList.Add(searchpath) RecurseFolders(searchpath) Else MessageBox.Show("Error: Folder does not exist", _ "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub
Using the FolderList, you can then loop through each folder and inspect the files within it.
To handle a shortcut, you'll also need this Class, which I called ShellMethods;
If you want to know how to then loop through all the files, here's an example of that which you can adapt to your own purpose;Code:Option Strict Off Friend Class ShellMethods Public Shared Function GetShortcutPath(ByVal FileName As String) As String Dim WSH As Object = CreateObject("WScript.Shell") Dim sc As Object = WSH.CreateShortcut(FileName) GetShortcutPath = sc.TargetPath End Function End Class
Code:Dim FolderIgnoredList As List(Of String) Public Function GetFileInformation(ByVal target As IO.DirectoryInfo) As IO.FileInfo() 'This sub returns the FileInfo for files in the target path Dim files As New List(Of IO.FileInfo) files.AddRange(target.GetFiles("*.*", IO.SearchOption.TopDirectoryOnly)) Return files.ToArray() End Function Private Sub WorkOnFiles(ByVal path As String) ' This sub does whatever you want to do with each file found ' It updates lists indicating files/folders that were ignored because of access problems GatherFolders(path) For Each folder As String In FolderList Dim ProblemPath As String FolderIgnoreList = New List(Of String) Try Dim folderinfo As New IO.DirectoryInfo(folder) Dim fileinfo() As IO.FileInfo = GetFileInformation(folderinfo) If fileinfo.Length > 0 Then For Each file As IO.FileInfo In fileinfo Try 'Do stuff with the file Catch ex As IO.***********Exception ProblemPath = IO.Path.GetFullPath(folder.ToString) If Not FolderIgnoredList.Contains(ProblemPath) Then FolderIgnoredList.Add(ProblemPath) Catch ex As System.UnauthorizedAccessException FolderFailedAccessList.Add(folder) Catch ex As Exception FolderFailedAccessList.Add(folder) End Try Next End If Catch ex As IO.***********Exception ProblemPath = IO.Path.GetFullPath(folder.ToString) If Not FolderIgnoredList.Contains(ProblemPath) Then FolderIgnoredList.Add(ProblemPath) Catch ex As System.UnauthorizedAccessException FolderFailedAccessList.Add(folder) Catch ex As Exception FolderFailedAccessList.Add(folder) End Try Next End Sub




Reply With Quote