4

有谁知道一种更快的方法来枚举目录和子文件夹以收集枚举中的所有文件?这就是我现在所拥有的:

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

谢谢!联合部队

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

4

4 回答 4

4

简短的回答:

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

长答案:

这段特定代码的快慢取决于很多因素。其中许多将取决于您正在运行的特定机器(例如硬盘驱动器速度)。查看只涉及文件系统的代码,很难肯定地说“x 比 y 快”。

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

如果这被证明是一个问题,那么您可以 PInvoke 进入 FindFirstFile / FindNextFile 并一次获取一个。结果在 CPU 周期中可能会在功能上变慢,但内存压力会更小。

但我必须强调,你应该在解决它们之前证明这些是问题。

于 2009-06-28T04:39:26.003 回答
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

于 2010-11-16T15:41:45.140 回答
0

这是一种粗略的做法。

dir /s /b

将其输出到文本文件中,读取并按\r\n.
在特定目录中运行上述命令,看看是否有帮助。

只获取目录

dir /s /b /ad

只获取文件

dir /s /b /a-d

编辑:贾里德说得对,除非你的方法被证明很慢,否则不要使用其他方法。

于 2009-06-28T05:40:14.803 回答
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
于 2011-12-06T14:05:34.133 回答