VB.NET directory.EnumerateFiles: Access to path X is denied

一曲冷凌霜 提交于 2019-12-12 01:14:27

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/42742018/vb-net-directory-enumeratefiles-access-to-path-x-is-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!