0

Ok so I am trying to write a program to find a specific file on the C drive and get its location. However the following code does not work! I've researched this a lot and gone from GetFiles to Directory.enumerateFiles. However I keep running into an exception claiming that I have no access, simply ignoring the message (closing it/pressing ok) does not continue the search and instead stops it altogether, I need a way of bypassing this if possible, so If a directory causes an exception it will skip it and move along with no error on screen if possible. Currently the manifest file is set to "requireAdministrator" so I know thats not the issue. Running VB as administrator does not solve, and running the compiled file as admin does not work either. Hope someone can help! Note: I'm using Visual Basic 2010 Express and have no plans to upgrade to a newever version due to hardware limitation and operating system.

Imports System.IO

Public Class GuardianScanner

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.ReadOnly = True
    TextBox1.ScrollBars = ScrollBars.Vertical

    Button1.Enabled = False
    TextBox1.AppendText(Environment.NewLine & "[INFO] Please wait while scanning. This can take a few minutes")
    Try

        For Each file As String In Directory.EnumerateFiles("C:\", "GodlyNorris.exe", SearchOption.AllDirectories)

                TextBox1.AppendText(Environment.NewLine & "[INFO] Found virus: AUTONORRIS: " & file.ToString)





        Next file

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
  • Is it throwing the exception in the TryCatch inside the For loop or in the outer TryCatch? – Chase Rocker Mar 11 '17 at 23:47
  • 3
    Likely it is trying to access a directory which you have no access to (there are lots of them on the OS drive). – Visual Vincent Mar 11 '17 at 23:48
  • outside the try the "for each file", as I say I need a way of bypassing where its not allowed to search – Daniel Oakley Mar 11 '17 at 23:48
  • @ChaseRocker : That must be the outside. The code in the inner `Try/Catch` is not capable of throwing such an exception since it is only performing string concatenation. – Visual Vincent Mar 11 '17 at 23:50
  • any idea on how to bypass searching the directories that are protected? – Daniel Oakley Mar 11 '17 at 23:52
  • 2
    Possible duplicate of [Directory.EnumerateFiles => UnauthorizedAccessException](http://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception) -- Different language, but easy to [**convert**](http://converter.telerik.com). – Visual Vincent Mar 11 '17 at 23:53
  • @DanielOakley that's a big **no**. Windows protects them for its own reasons, there's no bypassing them. For example; `Documents and Settings`. – Ally Mar 12 '17 at 11:34
  • I thing if you want to bypass the protected files, the 'For Each' loop will not work. Try using a loop with an integer.... somthing like `x +=1' when catching the error and re run the loop. In this way you will pass the protected files. – Mousa Alfhaily Mar 12 '17 at 12:09
  • Put the Try Catch inside the For each statement, and in the catch statement put Continue For. That way when you run into a directory that gives you the error, it will continue searching. – obl Mar 12 '17 at 21:40
  • @obl Yeah I tried that, however it stops searching when it catches the error, I left it for a couple hours but no it cancels the search. – Daniel Oakley Mar 13 '17 at 21:49

1 Answers1

3

I tried posting this as a comment but it's pretty messy. This should work, basically it tries to create a directory out of every subdirectory in the C drive, and if it fails with the unauthorized access exception, it moves on to the next subdirectory.

For Each directory In New DirectoryInfo("C:\").GetDirectories()
    Try
        For Each file In directory.GetFiles("*", SearchOption.AllDirectories)
            Try
                TextBox1.Text += Environment.NewLine + file.Name
            Catch ex As Exception
                'MsgBox(ex.Message)
                Continue For
            End Try
        Next
    Catch ex As Exception
        'MsgBox(ex.Message)
        Continue For
    End Try
Next
obl
  • 1,799
  • 12
  • 38
  • Ah thank you very much :) While it does halt the app whilst it searches it works just fine! I wasn't aware of Continue For even after researching! – Daniel Oakley Mar 15 '17 at 16:17