快速枚举所有文件,包括子文件夹

时间:2009-06-28 04:28:37

标签: vb.net winforms .net-3.5 file enumeration

有没有人知道通过目录和子文件夹枚举来更快地枚举枚举中的所有文件?这就是我现在所拥有的:

Public Shared allFiles() As String
allFiles = Directory.GetFiles(<ServerLocation>, "*.*", SearchOption.AllDirectories)

谢谢! JFV

编辑:我从服务器位置枚举这些文件。我不知道这是否会改变这个问题的观点。感谢到目前为止的所有输入!

4 个答案:

答案 0 :(得分:4)

简短回答:

如果此代码在功能上对您的项目是正确的,并且您没有证明它是分析器的问题,那么请不要更改它。继续使用功能正确的解决方案,直到证明它很慢。

答案很长:

这段特定代码的速度或速度有多快取决于很多因素。其中许多将取决于您运行的特定计算机(例如硬盘驱动器速度)。查看涉及文件系统的代码而不是其他内容,很难说“x比y更快”,具有任何确定性。

在这种情况下,我只能评论一件事。此方法的返回类型是FileInfo值的数组。数组需要连续的内存,非常大的数组可能会导致堆中的碎片问题。如果您正在阅读大型目录,则可能导致堆碎片化和间接性能问题。

如果这是一个问题,那么你可以PInvoke到FindFirstFile / FindNextFile并一次获取一个。结果可能在CPU周期中功能较慢,但内存压力较小。

但我必须强调,在你修复之前,你应该证明这些是问题。

答案 1 :(得分:3)

使用System.Collections.Generic;

private static List<string> GetFilesRecursive(string b)
{

             // 1.
            // Store results in the file results list.
            List<string> result = new List<string>();

            // 2.
            // Store a stack of our directories.
            Stack<string> stack = new Stack<string>();

            // 3.
            // Add initial directory.
            stack.Push(b);

            // 4.
            // Continue while there are directories to process
            while (stack.Count > 0)
            {
                // A.
                // Get top directory
                string dir = stack.Pop();

                try
                {
                    // B
                    // Add all files at this directory to the result List.
                    result.AddRange(Directory.GetFiles(dir, "*.*"));

                    // C
                    // Add all directories at this directory.
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                    }
                }
                catch
                {
                    // D
                    // Could not open the directory
                }
            }
            return result;
        }

道具原文:http://www.codeproject.com/KB/cs/workerthread.aspx

答案 2 :(得分:0)

这是一种粗暴的做法。

dir /s /b

将其输出到文本文件中,阅读它&amp;按\r\n分开。
在特定目录中运行以上命令,看看它是否有帮助。

仅获取目录

dir /s /b /ad

仅获取文件

dir /s /b /a-d

编辑:Jared说不要使用其他方法,除非你的方法被证明是缓慢的。

答案 3 :(得分:0)

继承我的解决方案。最初的启动有点慢,我正在努力。 my.computer.filesystem对象可能是启动缓慢的问题。但是这种方法将在5分钟内通过网络列出31,000个文件。

Imports System.Threading

Public Class ThreadWork

Public Shared Sub DoWork()
    Dim i As Integer = 1
    For Each File As String In My.Computer.FileSystem.GetFiles("\\172.16.1.66\usr2\syscon\S4_650\production\base_prog", FileIO.SearchOption.SearchTopLevelOnly, "*.S4")
        Console.WriteLine(i & ". " & File)
        i += 1
    Next
End Sub 'DoWork
End Class 'ThreadWork

Module Module1

Sub Main()
    Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
    Dim myThread As New Thread(myThreadDelegate)
    myThread.Start()
    '"Pause" the console to read the data.
    Console.ReadLine()
End Sub 'Main

End Module
相关问题