代码之家  ›  专栏  ›  技术社区  ›  JFV

枚举所有文件(包括子文件夹)的快速方法

  •  5
  • JFV  · 技术社区  · 17 年前

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

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

    4 回复  |  直到 16 年前
        1
  •  4
  •   JaredPar    17 年前

    简短回答:

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

    长答案:

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

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

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

    但我必须强调,你应该证明这些都是问题 修复 他们。

        2
  •  3
  •   user430728    15 年前

    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

        3
  •  0
  •   shahkalpesh    17 年前

    这是一种粗鲁的做法。

    dir /s /b
    

    将此输出转换为文本文件,读取它&按...分割 \r\n .
    在特定目录中运行上述命令,看看是否有帮助。

    dir /s /b /ad
    

    只获取文件

    dir /s /b /a-d
    

        4
  •  0
  •   Rob    14 年前

    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