0

Visual Studio 2008 (vb.net) 我制作了简单的 anivirus,但是当我通过这段代码进行全面扫描时:

FolderBrowserDialog1.SelectedPath = ("C:\")
    'first scan:************************************
    Try

        For Each strDir As String In
        System.IO.Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)

            For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                ListBox1.Items.Add(strFile)

            Next
        Next

        'Start the timer:

    Catch ex As Exception
    End Try

    Timer1.Start()`

只需扫描前 6 个文件...我认为问题来自 Windows 文件夹权限(Windows - 程序文件...等)那么如何解决呢?

4

2 回答 2

2

将 aConsole.WriteLine(ex)在您的 catch 块中,以便您可以看到抛出的任何异常。那时你可能会看到你的问题。最有可能的权限。

于 2012-04-04T12:37:20.787 回答
1

您可以尝试以下方法:

For Each strFile As String In System.IO.Directory.GetFiles(strDir, "*", IO.SearchOption.AllDirectories)

编辑:

您可以尝试在此线程中找到的最后一个解决方案: http ://www.vbforums.com/showthread.php?t=624969

我自己尝试过,它非常慢,但效果很好。

Public Class Form1

    Private Sub foo(ByVal aDir As String)
        Try

            Dim di As New IO.DirectoryInfo(aDir)
            Dim aryFiles() As IO.FileInfo = di.GetFiles("*.*")
            Dim aryDirs() As IO.DirectoryInfo = di.GetDirectories()

            For Each fi As IO.FileInfo In aryFiles
                rslts.Add(fi.FullName)
            Next

            For Each d As IO.DirectoryInfo In aryDirs
                foo(d.FullName)
            Next

        Catch ex As Exception
            'Stop 'the catch should be more specific
        End Try

    End Sub

    Dim rslts As List(Of String)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        rslts = New List(Of String)
        foo("C:\")
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(rslts.ToArray)
     End Sub
End Class

看起来您的解决方案本质上是循环遍历它可以找到的第一个文件夹并停在那里。这个解决方案有点不同,因为它会根据起始位置递归地遍历所有文件和文件夹。

于 2012-04-04T12:10:36.850 回答