简体   繁体   English

C# 有没有比 FileInfo.Length 更快的方法来比较多个文件?

[英]C# Is there a faster way than FileInfo.Length with multiple files comparison?

I have been trying to compare files from two differents environments.我一直在尝试比较来自两个不同环境的文件。 Both environment are accessed via a network drive.两种环境都通过网络驱动器访问。

First, I checked if the file is present in both environments (to save time), then I ask for a FileInfo on both files to compare their filesizes (FileInfo.Length).首先,我检查了文件是否存在于两个环境中(以节省时间),然后我要求两个文件上的 FileInfo 以比较它们的文件大小(FileInfo.Length)。

My goal is to populate a listview with every files that does not have the same size to investigate on these later.我的目标是用每个大小不同的文件填充一个列表视图,以便以后对这些文件进行调查。

I find it hard to understand that windows explorer gets the filesize of a lot of files so quickly and that FileInfo is taking so long...我发现很难理解 windows 资源管理器如此快速地获取大量文件的文件大小,而 FileInfo 需要这么长时间......

Thanks.谢谢。 Ben

If you use DirectoryInfo.GetFiles( some-search-pattern ) to get the files, then use the FileInfo-instance returned from that call, then the property FileInfo.Length will be cached from the search.如果您使用 DirectoryInfo.GetFiles( some-search-pattern ) 获取文件,然后使用从该调用返回的 FileInfo-instance,则属性 FileInfo.Length 将从搜索中缓存。

Whether this will be of any help obviously depends on how the search performs in comparison to the check you are currently doing (if the search is slower, you may not gain anything).这是否有任何帮助显然取决于与您当前正在进行的检查相比搜索的执行情况(如果搜索速度较慢,您可能不会获得任何东西)。 Still, it may be worth looking into.不过,它可能值得研究。

You can try doing it the operating system way.您可以尝试以操作系统的方式进行操作。 You call via win32 system calls.你通过win32系统调用调用。 You can start with this page,你可以从这个页面开始,

http://www.pinvoke.net/default.aspx/kernel32.getfilesize http://www.pinvoke.net/default.aspx/kernel32.getfilesize

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM