递归文件搜索代码崩溃的问题 [英] Problem With Recursive File Searching Code Crashing

查看:177
本文介绍了递归文件搜索代码崩溃的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在编写一个简单的程序,以通过硬盘进行递归搜索并返回所有当前文件的列表.我在一个简单的反病毒应用程序中使用它.我编写了以下函数来搜索文件并将它们添加到列表框中:

Hi I am writing a simple programme to search recursively through a hard drive and return a list of all present files. I am using this in a simple Anti-Virus application. I have written the following function for searching through the files and adding them to a list box:

'The following function is called with the parameter "C:\" or whatever drive i want it to search
Private Function RunScan(ByVal Folder As String) As String
    Dim sReturn As String
    Dim Path As String
    Dim File As String
    Dim Location As New IO.DirectoryInfo(Folder)
    Dim files As IO.FileInfo() = Location.GetFiles()
    Dim CurrentFile As IO.FileInfo

    sReturn = Folder

    For Each Path In IO.Directory.GetDirectories(Folder)
        sReturn &= ControlChars.CrLf & RunScan(Path)
    Next

    For Each File In IO.Directory.GetFiles(Folder)
        sReturn &= ControlChars.CrLf & File
    Next

    For Each CurrentFile In files
'Add every file searched to ListBox 1
        ListBox1.Items.Add(CurrentFile)
    Next
    Return sReturn
End Function



该代码在用于扫描较小的驱动器(例如闪存驱动器)时非常有效,但是一旦用于扫描较大尺寸的硬盘时,程序将变得无响应并崩溃,即使我尝试使用它也不会返回任何错误尝试在代码中使用Catch语句.

如果有人有任何想法,我将不胜感激,在此先感谢您:)



The Code works perfectly when used to scan a small drive such as a flash drive but as soon as it is used to scan a hard drive of larger size the programme becomes unresponsive and crashes, it does not return any errors even when I try to use Try Catch statements within the code.

If anyone has any ideas I would be very grateful to hear from you, Thanks in advance :)

推荐答案

很明显,您正在使用一堆东西内存,您需要进行大量处理.
It''s obvious that you''re using a bunch of memory and you''re doing a lot of processing.
ListBox1.Items.Add(CurrentFile)


通过该行,您将为指定路径中的每个文件保存一个文件信息对象,并将其添加到列表框中.那会消耗很多内存.另外,您可能要等到完成扫描后才能一次将所有项目添加到列表框中(我认为有一种特殊的方法可以做到,这样一件一件地添加项目不会使事情变慢.)


With that line, you are saving a file info object for every file in the path specified, and you are adding it to a listbox. That could consume a lot of memory. Also, you may want to wait until you are done scanning to add the items to the listbox all at once (I think there is a special way to do that so adding items one by one doesn''t slow things down so much).

sReturn &= ControlChars.CrLf & RunScan(Path)
sReturn &= ControlChars.CrLf & File


通过这些行,您可以将一个字符串连接到一个已经很大的字符串.这将创建一个全新的字符串,并且都消耗内存,直到旧字符串被垃圾回收为止.每次执行此操作都将执行字符串连接.查看StringBuilder以优化此设置并防止不必要的字符串连接.通过执行此操作,您的程序将以N倍(其中N是文件数)的性能更好.

另外,您说过您的用户界面已锁定.这是因为您正在UI线程上运行所有这些代码.如果您在新线程上运行它,并且仅将更新发送到UI线程,则您的UI将不会锁定.


With these lines, you are concatenating a string to an already large string. This creates an entirely new string and both consume memory until the old string gets garbage collected. You do this EACH TIME you perform the string concatenation. Look into StringBuilder to optimize this and prevent the unnecessary string concatenations. You''re program will perform better by a factor of N (where N is the number of files) by doing this.

Also, you said your UI locks up. This is because you are running all this code on the UI thread. If you run it on a new thread and only send updates to the UI thread, your UI will not lock up.


这篇关于递归文件搜索代码崩溃的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆