有没有比枚举文件更快的列出文件夹和文件的方法? [英] Is there a faster way to list folders and files than enumeratefiles?

查看:77
本文介绍了有没有比枚举文件更快的列出文件夹和文件的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录列表(121),我想生成它们内部的所有文件,包括子目录。原始列表中有大约7300个文件和文件夹。 EnumerateFiles和GetFiles将永远填充列表。 (8分钟)我已经完成了一个cmd行Dir / s和一个最大目录的enumeratefiles的比较。 enumeratefiles大约需要35秒,Dir / s大约需要2秒。我只是在寻找一个包含完整路径名的文件和文件夹列表,例如你从cmd行Dir / s获得的内容。我有更快的方法来获取此列表吗?我一直在尝试VBDir(文件名),但似乎无法让它工作。原始文件列表来自我之前填充的数组。我是新手,所以我可能没有使用良好的编码实践。 :/



我尝试过:



每个文件名为字符串在IO.Directory.EnumerateFiles(o:\ tapes \ maximim500\dnc,*,IO.SearchOption.AllDirectories)



如果TextBox1 .Text =然后

TextBox1.Text =文件名

否则

TextBox1.Text = TextBox1.Text& vbNewLine&文件名

结束如果





下一步

I have a list of directories (121) that I want to generate all the files inside of them including sub directories. There are about 7300 files and folders inside the original list. EnumerateFiles and GetFiles take forever to populate the list. (8 minutes) I have done a comparison of a cmd line "Dir /s" and the "enumeratefiles" for one of the largest directories. "enumeratefiles" takes about 35 seconds, and "Dir /s" takes 2 seconds. I am just looking for a list of files and folders with their complete path name such as what you get from the cmd line "Dir /s". Is there a faster method for me to get this list? I have been trying the VB "Dir(filename)" but cannot seem to get this to work. The original list of files is coming from an array that I have previously populated. I'm new at this so I am probably not using good coding practices. :/

What I have tried:

For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)

If TextBox1.Text = "" Then
TextBox1.Text = filename
Else
TextBox1.Text = TextBox1.Text & vbNewLine & filename
End If


Next

推荐答案

你经常连接字符串,这可以解释你得到的糟糕表现。

你应该使用StringBuilder来构造字符串,并在最后将它分配给text属性。

You are constantly concatenating strings, which can explain the poor performance you get.
You should use a StringBuilder to construct the string, and assign it to the text property at the end.
Dim sb As new System.Text.StringBuilder(500000)
For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
   sb.AppendLine(filename)
Next
TextBox1.Text = sb.ToString()



希望这会有所帮助。


Hope this helps.


我不确定为什么要在文本框中显示那么多信息。附加到文本框是一种非常缓慢且资源密集的方法。你应该使用StringBuilder。



一个更好的方法,只是稍微快一点就是使用一个列表框。这也使您能够选择项目。



I am not sure why you want to display that much information in a textbox. Appending to a textbox is a very slow and resource intensive way to do it. You should use StringBuilder instead.

A better method, only slightly faster is to use a listbox. This also gives you the ability to select items.

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

    Dim sb As StringBuilder = New StringBuilder()

    For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
        sb.AppendLine(filename)
    Next

    TextBox1.Text = sb.ToString()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ListBox1.DataSource = IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories).ToList
End Sub





最快的方法是使用一个TreeView和延迟加载按需扩展的文件夹。



The fastest way to do it is to use a TreeView and lazy load on demand the folder that is expanded.


您正在通过添加每个文件名来更新文本框。这会耗费大量时间,因为对于每个文件名,文本框内容必须更新并重新绘制。



您可以将名称附加到 StringBuilder 变量(比使用 String 更快)并在确定所有名称后将其分配给文本框:

You are updating a text box by adding each file name. That consumes a lot of time because for each file name the text box content must be updated and redrawn.

You can append the names to a StringBuilder variable instead (which is faster than using a String) and assign that to the text box after all names has been determined:
Dim listOfNames As New StringBuilder
For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
    If listOfNames.Length > 0 Then
        listOfNames.AppendLine()
    End If
    listOfNames.Append(filename)
Next
TextBox1.Text = listOfNames.ToString()



如果要在枚举时定期更新文本框,请添加计数器变量并更新跨越边界的文本框(可以使用 Mod 运算符完成)。


这篇关于有没有比枚举文件更快的列出文件夹和文件的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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