Results 1 to 8 of 8

Thread: Searching All Files (Access Denied)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Searching All Files (Access Denied)

    Hello here is my code:

    vb.net Code:
    1. Module FileCheck
    2.     Public BW_AntiCorrupt As New Threading.Thread(AddressOf AntiCorrupt)
    3.     Dim THardDrive As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    4.     Dim HardDrive As String = THardDrive.Substring(0, THardDrive.IndexOf("\") + 1) 'C:\
    5.     Public ContinueSearch As Boolean = True
    6.     Public Results As New List(Of String)
    7.     Public DoneGather As Boolean = False
    8.     Public DiagnosticReport As String
    9.  
    10.     Public Sub AntiCorrupt()
    11.         Dim DirInfo As New IO.DirectoryInfo(HardDrive)
    12.         DoneGather = False
    13.         Results.AddRange(IO.Directory.GetFiles(HardDrive, "*.*", IO.SearchOption.AllDirectories))
    14.         DoneGather = True
    15.         For Each file As String In Results
    16.             If ContinueSearch = True Then
    17.                 CheckFile(file)
    18.             End If
    19.         Next
    20.     End Sub
    21.     Private Sub CheckFile(ByVal file As String)
    22.         Dim finfo As New IO.FileInfo(file)
    23.  
    24.     End Sub
    25. End Module

    This code is being called wit this code:
    vb.net Code:
    1. BW_AntiCorrupt.IsBackground = True
    2.                 BW_AntiCorrupt.Start()
    3.                 frmFileCheckResults.Show()

    I am having a problem. The IO.Directory.GetFiles code is resulting in error do to the fact that it is trying to access the Temp folder in the Windows folder on drive C:\. Apparently that folder is restricted... When the code encounters that folder the code stops retrieving files.

    Is there a way around this? Is there somewhere i can give my program administrator access? If not then am i able to skip the files in that location?
    Last edited by noahssite; Jun 6th, 2009 at 10:43 AM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Searching All Files (Access Denied)

    Would i put this code at the first line of the AntiCorrupt sub?

    Code:
    Dim i As Security.Permissions.FileIOPermission
            i.AllFiles = Security.Permissions.FileIOPermissionAccess.AllAccess
    I didn't want to test it because of my next question.
    Does this just change the access MY program has? Or does it change the access permently on the computer in general? Or does this code not even work?

  3. #3
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Searching All Files (Access Denied)

    When i tried your code at one point i got an exception when the search got to the folder of the program, i think it got something when the program try to "touch" the file while it in use.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Searching All Files (Access Denied)

    If that is the case what should i do about it? Also i think it might also be admin access (im on a vista). Is there a way to run the code as administrator?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Searching All Files (Access Denied)

    I ran a few tests and i am sure it is the access. I went to the debug exe in my application's folder and ran it as admin and it worked up until a system folder. So my program doesn't just need admin access? It needs system access? or is it just something with file permissions?

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Searching All Files (Access Denied)

    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;

    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
    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.

    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;

    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
    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:
        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
    Last edited by Bulldog; Jun 6th, 2009 at 02:30 PM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Searching All Files (Access Denied)

    Thank you but:

    Quote Originally Posted by Bulldog
    Unless you're the Admin, you won't get access.
    I am the admin.

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Searching All Files (Access Denied)

    ok, but your comment was;
    If not then am i able to skip the files in that location?
    So I showed you how write code to skip that location.

    If you're the Admin and running on Vista then take a look at this thread http://www.vbforums.com/showthread.php?t=556963

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width