搜索文件的最有效方法

发布于 2024-12-11 17:01:02 字数 429 浏览 0 评论 0原文

我正在编写一个程序,用于搜索 mp3 文件并将其复制到指定目录。

目前,我正在使用一个包含目录中所有 mp3 的列表(毫不奇怪,这需要很长时间。)然后我使用 taglib-sharp 将 ID3Tags 与输入的艺术家和标题进行比较。如果它们匹配,我复制该文件。

由于这是我的第一个程序,而且我对编程非常陌生,我认为必须有更好/更有效的方法来做到这一点。有人对我可以尝试什么有建议吗?

编辑:我忘记添加一个重要的细节:我希望能够指定每次开始搜索时应搜索哪些目录(要搜索的目录将在程序本身中指定)。因此,将所有文件存储在数据库或类似的东西中并不是真正的选择(除非有一种方法可以每次都执行此操作并且仍然有效)。我基本上正在寻找最好的方法来搜索每次对文件进行索引的目录中的所有文件。 (我知道这可能不是一个好主意,但我想这样做。如果没有真正的方法可以做到这一点,我将不得不重新考虑,但现在我想这样做.)

I am writing a program that searches and copies mp3-files to a specified directory.

Currently I am using a List that is filled with all the mp3s in a directory (which takes - not surprisingly - a very long time.) Then I use taglib-sharp to compare the ID3Tags with the artist and title entered. If they match I copy the file.

Since this is my first program and I am very new to programming I figure there must be a better/more efficient way to do this. Does anybody have a suggestion on what I could try?

Edit: I forgot to add an important detail: I want to be able to specify what directories should be searched every time I start a search (the directory to be searched will be specified in the program itself). So storing all the files in a database or something similar isn't really an option (unless there is a way to do this every time which is still efficient). I am basically looking for the best way to search through all the files in a directory where the files are indexed every time. (I am aware that this is probably not a good idea but I'd like to do it that way. If there is no real way to do this I'll have to reconsider but for now I'd like to do it like that.)

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

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

发布评论

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

评论(2

仅此而已 2024-12-18 17:01:02

您主要面临 IO 瓶颈,这是您所使用的硬件的结果。此处的分母是文件的复制(除了查找文件,这与复制相比微不足道)。

还有其他方法可以进行文件管理,每种方法都为不同的目的提供更好的界面,例如NTFS 更改日志 和低级扇区处理(不推荐),但如果这是您的第一个程序在 C# 中,那么您可能不想冒险进行 p/invoking 本机调用。

除了实际流程的替代方案之外,您还可以考虑最小化磁盘访问的机制 - 即不重做任何您已经完成或不需要做的事情。

You are mostly saddled with the bottleneck that is IO, a consequence of the hardware with which you are working. It will be the copying of files that is the denominator here (other than finding the files, which is dwarfed compared to copying).

There are other ways to go about file management, and each exposing better interfaces for different purposes, such as NTFS Change Journals and low-level sector handling (not recommended) for example, but if this is your first program in C# then maybe you don't want to venture into p/invoking native calls.

Other than alternatives to actual processes, you might consider mechanisms to minimise disk access - i.e. not redoing anything you have already done, or don't need to do.

趁年轻赶紧闹 2024-12-18 17:01:02

使用数据库(简单的二进制序列化文件或嵌入式数据库,如 RavenDb)来缓存所有文件。并查询该缓存。

还将每个文件夹的修改时间存储在数据库中。每次启动应用程序(并同步更改的文件夹)时,将数据库中的时间与文件夹中的时间进行比较。

这应该会给你带来更好的性能。线程并不能真正帮助搜索文件夹,因为需要时间的是磁盘 IO,而不是您的应用程序。

Use an database (simple binary serialized file or an embedded database like RavenDb) to cache all files. And query that cache instead.

Also store modified time for each folder in the database. Compare the time in the database with the time on the folder each time you start your application (and sync changed folders).

That ought to give you much better performance. Threading will not really help searching folders since it's the disk IO that takes time, not your application.

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