仅访问目录时获取目录下所有文件的属性

发布于 2024-12-02 04:28:34 字数 1295 浏览 0 评论 0原文

我正在尝试用 C# 编写一个函数,该函数获取目录路径作为参数并返回一个字典,其中键是直接在该目录下的文件,值是它们的最后修改时间。 使用 Directory.GetFiles() 和 File.GetLastWriteTime() 可以轻松完成此操作。然而,这意味着必须访问每个文件,这对于我的需求来说太慢了。 有没有办法在仅访问目录时执行此操作?文件系统是否支持这种要求?

阅读一些答案后进行编辑: 谢谢你们,你们说的几乎都是一样的——使用 FileInfo 对象。尽管如此,使用 Directory.GetFiles() (或 Directory.EnumerateFiles()) 获取这些对象也同样慢,而且我怀疑获取它们需要访问每个文件。如果文件系统仅将其文件的上次修改时间保留在文件本身中,则无法在没有文件访问的情况下提取该信息。这里是这样吗? DirectoryInfo 的 GetFiles() 和 EnumerateFiles() 是否访问每个文件或从目录条目获取其信息?我知道,如果我只想获取文件名,我可以使用 Directory 类来执行此操作,而无需访问每个文件。但获取属性似乎更棘手......

编辑,遵循亨克的回应: 看来使用 FileInfo 对象确实更快。我创建了以下测试:

static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now);

        foreach (string file in Directory.GetFiles(@"\\169.254.78.161\dir"))
        {
            DateTime x = File.GetLastWriteTime(file);
        }

        Console.WriteLine(DateTime.Now);

        DirectoryInfo dirInfo2 = new DirectoryInfo(@"\\169.254.78.161\dir");
        var files2 = from f in dirInfo2.EnumerateFiles()
                select f;
        foreach (FileInfo file in files2)
        {
            DateTime x = file.LastWriteTime;
        }

        Console.WriteLine(DateTime.Now);
    }

对于大约 800 个文件,我通常会得到如下结果:
2011年8月31日 17:14:48
2011年8月31日 17:14:51
31/08/2011 17:14:52

I'm trying to write a function in C# that gets a directory path as parameter and returns a dictionary where the keys are the files directly under that directory and the values are their last modification time.
This is easy to do with Directory.GetFiles() and then File.GetLastWriteTime(). However, this means that every file must be accessed, which is too slow for my needs.
Is there a way to do this while accessing just the directory? Does the file system even support this kind of requirement?

Edit, after reading some answers:
Thank you guys, you are all saying pretty much the same - use FileInfo object. Still, it is just as slow to use Directory.GetFiles() (or Directory.EnumerateFiles()) to get those objects, and I suspect that getting them requires access to every file. If the file system keeps last modification time of its files in the files themselves only, there can't be a way to extract that info without file access. Is this the case here? Do GetFiles() and EnumerateFiles() of DirectoryInfo access every file or get their info from the directory entry? I know that if I would have wanted to get just the file names, I could do this with the Directory class without accessing every file. But getting attributes seems trickier...

Edit, following henk's response:
it seems that it really is faster to use FileInfo Object. I created the following test:

static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now);

        foreach (string file in Directory.GetFiles(@"\\169.254.78.161\dir"))
        {
            DateTime x = File.GetLastWriteTime(file);
        }

        Console.WriteLine(DateTime.Now);

        DirectoryInfo dirInfo2 = new DirectoryInfo(@"\\169.254.78.161\dir");
        var files2 = from f in dirInfo2.EnumerateFiles()
                select f;
        foreach (FileInfo file in files2)
        {
            DateTime x = file.LastWriteTime;
        }

        Console.WriteLine(DateTime.Now);
    }

For about 800 files, I usually get something like:
31/08/2011 17:14:48
31/08/2011 17:14:51
31/08/2011 17:14:52

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

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

发布评论

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

评论(2

为你拒绝所有暧昧 2024-12-09 04:28:34

我没有做任何计时,但最好的选择是:

DirectoryInfo di = new DirectoryInfo(myPath);
FileInfo[] files = di.GetFiles();

我认为所有 FileInfo 属性都在目录文件记录中可用,因此这应该(可能)需要最少的 I/O。

I didn't do any timings but your best bet is:

DirectoryInfo di = new DirectoryInfo(myPath);
FileInfo[] files = di.GetFiles();

I think all the FileInfo attributes are available in the directory file records so this should (could) require the minimum I/O.

分開簡單 2024-12-09 04:28:34

我唯一能想到的就是使用 FileInfo-Class。据我所知,这可能对您有帮助,或者它也可能读取该文件(需要读取权限)

The only other thing I can think of is using the FileInfo-Class. As far as I can see this might help you or it might read the file as well (Read Permissions are required)

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