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

发布于 2024-07-25 19:57:25 字数 289 浏览 3 评论 0原文

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

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

谢谢! JFV

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

Does anyone know of a faster way to enumerate through a directory and sub-folders to gather all the files in the enumeration? This is what I have right now:

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

Thanks!
JFV

EDIT: I am enumerating these files from a server location. I don't know if that will change the perspective of this question or not. Thanks for all the input so far!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春庭雪 2024-08-01 19:57:26

这是我的解决方案。 最初的启动有点慢,我正在努力。 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

…………

Heres my solution. The initial start up is a little slow, im working on that. The my.computer.filesystem object is probabbly the problem for the slow start up. But this method will list 31,000 files in under 5 minutes over a Network.

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

.......

海风掠过北极光 2024-08-01 19:57:25

简短的回答:

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

长答案:

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

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

如果这确实是一个问题,那么您可以 PInvoke 进入 FindFirstFile / FindNextFile 并一次获取它们。 结果可能会导致 CPU 周期的功能变慢,但内存压力会较小。

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

Short answer:

If this code is functionally correct for your project and you haven't proved it to be a problem with a profiler then don't change it. Continue to use a functionally correct solution until you prove it to be slow.

Long answer:

How fast or slow this particular piece of code is depends on a lot of factors. Many of which will depend on the specific machine you are running on (for instance hard drive speed). Looking at code that involves the file system and nothing else, it's very difficult to say "x is faster than y" with any degree of certainty.

In this case, I can only really comment on one thing. The return type of this method is an array of FileInfo values. Arrays require contiguous memory and very large arrays can cause fragmentation issues in your heap. If you have extremely large directories that you are reading it could lead to heap fragmentiation and indirectly performance issues.

If that turns out to be a problem then you can PInvoke into FindFirstFile / FindNextFile and get them one at a time. The result will be likely functionally slower in CPU cycles but will have less memory pressure.

But I must stress that you should prove these are problems before you fix them.

原野 2024-08-01 19:57:25

使用 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

using 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;
        }

Props to the original article: http://www.codeproject.com/KB/cs/workerthread.aspx

网白 2024-08-01 19:57:25

这是一种粗略的做法。

dir /s /b

将其输出放入文本文件中,读取它并进行编辑。 按 \r\n 分割。
在特定目录中运行上述命令,看看是否有帮助。

只获取目录

dir /s /b /ad

只获取文件

dir /s /b /a-d

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

This is a crude way of doing it.

dir /s /b

Get the output of this into a text file, read it & split by \r\n.
Run the above command in a specific directory, to see if it helps.

To only get the directories

dir /s /b /ad

To only get the files

dir /s /b /a-d

EDIT: Jared is right in saying not to use other approaches unless your approach is proved slow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文