locked
continuing file search after UnauthorizedAccessException error RRS feed

  • 問題

  •  

    I want to search an perticular .exe file on all the fixed drives (c:, d:, ....) and then replace it with another same named file. I am trying to get it but could not.

    With try catch method the search is stopped with 'UnauthorizedAccessException'. I have given the following code

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim folder As String = "c:\"
            Dim pattern As String = "example.exe"


            Try
                For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)
                    MessageBox.Show(file)
                Next
            Catch ex As UnauthorizedAccessException
                MsgBox(Err.Description)
            End Try

        End Sub
    End Class


    masoom sanadi.

    2006年11月21日 上午 10:43

解答

  • you should place the try catch block within the for each block:

    For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

       Try

            MessageBox.Show(file)

            Catch ex As UnauthorizedAccessException
                MsgBox(Err.Description)
            End Try

    Next

     

    this will catch the error inside and still continue onwards with the for each loop.

    2006年11月21日 下午 01:46
  • you cant go into directories that give you the unauthorizedAccessException - you wont have permission to go into it, this is what it is saying.

    you should also catch specific exceptions than a general exception as a general exception catch is expensive. Generally the possible exceptions that can occur with this is the unauthorizedaccessException and IOException

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

              Dim folder As String = "c:\"

              Dim pattern As String = "gta_vc.set"

     For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

                  

              Try

                        MessageBox.Show(file)

                        Beep()

     

              Catch ex As UnauthorizedAccessException

                   MsgBox(ex.Message)

       Catch ex as IOException

       MessageBox.show(ex.ToString())

              End Try

                   Next

         End Sub

    2006年11月22日 下午 07:40
  • ok. You need to do a recursive function. Get a list of directories in the top root only, then go through each directory inside the current top level directory and do your thing. Something like.....

    private theCollectionOfFilesFound as new ArrayList() 'global

     

    private sub DoSearch(byval rootPath as string)

       Dim folders() as String = Directory.GetDirectories(rootPath)

       for each currentDir as String in folders

          try

             Me.DoSearch(currentDir)

             Me.DoFindfile(currentDir)

          catch ex as UnauthorizedException

             'handle

          end try

       next

    end sub

     

    private sub DoFindFile(byval currentPath as String)

       Dim theFilesFound() as String = Directory.GetFiles(currentPath, "searchPattern", SearchOption.TopDirectoryOnly)

       for each currentFile as string in theFilesFound

          Me.theCollectionOfFilesFound.Add(currentFile)

       next

    end sub

    2006年11月22日 下午 09:19

所有回覆

  • you should place the try catch block within the for each block:

    For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

       Try

            MessageBox.Show(file)

            Catch ex As UnauthorizedAccessException
                MsgBox(Err.Description)
            End Try

    Next

     

    this will catch the error inside and still continue onwards with the for each loop.

    2006年11月21日 下午 01:46
  • Dear IlyasAhmed thank you for your suggestion. i tried what you have written but didn't found it to be a solution.

    As the following code

                                                        For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

    has searching code line prior to Try and Catch block it throws the error "Access to the path 'c:\system volume information' is denied." and the search is stopped, which is my exact problem.

    I want to continue searching all the sub directores/folders may there be any exception. Any other exception. i.e. all the exceptions must be ignored and the search sould continue.

    So please help me again.

    Example code:

    Public Class Form1

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

              Dim folder As String = "c:\"

              Dim pattern As String = "gta_vc.set"

              Try

                   For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

                        MessageBox.Show(file)

                        Beep()

                   Next

              Catch ex As UnauthorizedAccessException

                   MsgBox(ex.Message)

              End Try

         End Sub

    End Class

    2006年11月22日 下午 07:04
  • you cant go into directories that give you the unauthorizedAccessException - you wont have permission to go into it, this is what it is saying.

    you should also catch specific exceptions than a general exception as a general exception catch is expensive. Generally the possible exceptions that can occur with this is the unauthorizedaccessException and IOException

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

              Dim folder As String = "c:\"

              Dim pattern As String = "gta_vc.set"

     For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

                  

              Try

                        MessageBox.Show(file)

                        Beep()

     

              Catch ex As UnauthorizedAccessException

                   MsgBox(ex.Message)

       Catch ex as IOException

       MessageBox.show(ex.ToString())

              End Try

                   Next

         End Sub

    2006年11月22日 下午 07:40
  • i understand what you r telling. But what i want to say is, as the line

    For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchAllSubDirectories, pattern)

    starts before try and catch block the search ends before the entire drive is searched or to be specific the search ends with 'UnauthorizedAccessException' error. I want my search to be continued even after this exception or even after any exception.

    Time required or time performance is not the issue for the client.

    2006年11月22日 下午 08:01
  • ok. You need to do a recursive function. Get a list of directories in the top root only, then go through each directory inside the current top level directory and do your thing. Something like.....

    private theCollectionOfFilesFound as new ArrayList() 'global

     

    private sub DoSearch(byval rootPath as string)

       Dim folders() as String = Directory.GetDirectories(rootPath)

       for each currentDir as String in folders

          try

             Me.DoSearch(currentDir)

             Me.DoFindfile(currentDir)

          catch ex as UnauthorizedException

             'handle

          end try

       next

    end sub

     

    private sub DoFindFile(byval currentPath as String)

       Dim theFilesFound() as String = Directory.GetFiles(currentPath, "searchPattern", SearchOption.TopDirectoryOnly)

       for each currentFile as string in theFilesFound

          Me.theCollectionOfFilesFound.Add(currentFile)

       next

    end sub

    2006年11月22日 下午 09:19
  • Dear AhmedIlyas

    Thank u very much for helping me out

    You code helped me out to get the result

    My problem is solved but one thing still i want to get cleared is that

    How to catch any exception that will be encountered during a program.

     

    My exact problem is solved so THANKS, ONCE AGAIN.

    2006年11月25日 下午 07:46
  • no worries.

    well to catch any exception, you REALLY should place try catch blocks around the part where you think the exception will occur but better yet to look in the MSDN docs for the method in question and see what exceptions it throws so you can catch it and handle it and move on.

    this would be the best way

    2006年11月25日 下午 07:50
  • The problem is that the method Directory.GetFiles() is missing a parameter like [ bool ignoreExceptions ] that allows you to do deep searches thru the drive.

    If you end doing yourself a search file method what is the sense of having a Directory.GetFiles()?

    As you don't know at compile time when you will get an UnauthorizedAccessException you can't use the Directory.GetFiles() never.
    2008年6月5日 上午 11:03
  • I totally agree with somos. It's a nonsense to propose a function like GetFiles if it's unable to deal with system protected directories. Moreover when those directories are created by Windows itself.
    That's really a pity because the recursive GetFile function is really interesting but totally useless bacause of this limitation. And that's a pain to write his own function...
    Isn't there any way just to ignore those unauthorized directories and carry on with the next ones?
    2009年3月20日 下午 09:01
  • Notably when the GetFiles or GetDirectories is trying to access a mapped drive where the files are stored on a Linux or Mac server.  I am getting "Illegal characters in path" error because the non-MS OSs allow for chars that MS does not.  If I could have something like the boolean "ignore errors" suggestion above, that would be the best solution.
    2011年11月28日 上午 11:36
  • I don't know why it's mark as a solution but it's not the case. The exception happened in the GetFiles function. The try catch in following the for cannot be hit. There is no way to use the function at all. You must code your own.
    2020年10月12日 上午 01:04
  • Indeed.

    However that reply is marked as answer before the forum software is modified to log who mark the post as answer, so we have no way to figure out.

    If forum moderators see this, please help unmark the first and second "answer" of ahmedilyas, the third answer has no such problem though.


    2020年10月12日 上午 01:16
    解答者