磁盘碎片整理如何工作?

发布于 2024-08-20 20:35:21 字数 174 浏览 11 评论 0原文

我想尝试写一些东西来显示硬盘驱动器的碎片程度。也许甚至可以尝试对其进行碎片整理。

但我意识到我并不完全理解这是如何运作的。

谁能向我解释一下这一点,也许可以提供一些我可以从哪里开始的建议?

我主要使用 C#——这是否是一种合适的语言来尝试将一些东西组合在一起。

提前致谢

I'd like to have a go at writing something which shows the state of a hard drive in terms of how fragmented it is. Maybe even has a go at de-fragmenting it.

But I've realised that I don't fully understand how this works.

Can anyone explain this to me and perhaps offer some suggestions of where I might start?

I mainly use C# - would this be a suitable language to have a go at putting something together.

Thanks in advance

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

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

发布评论

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

评论(3

阳光的暖冬 2024-08-27 20:35:21

请从维基百科关于磁盘碎片的文章开始,

然后,这取决于您的级别有多低想去。

首先,请参阅对文件进行碎片整理的官方操作方法MSDN。

从文章中....

  1. 使用 FSCTL_GET_VOLUME_BITMAP 控制代码在卷上查找足够大以容纳整个文件的位置。
    注意 如有必要,请移动其他文件以腾出足够大的位置。理想情况下,文件的第一个范围之后有足够的未分配簇,您可以将后续范围移动到第一个范围之后的空间中。
  2. 使用 FSCTL_GET_RETRIEVAL_POINTERS 控制代码获取磁盘上文件当前布局的映射。
  3. 遍历 FSCTL_GET_RETRIEVAL_POINTERS 返回的 RETRIEVAL_POINTERS_BUFFER 结构。
  4. 使用 FSCTL_MOVE_FILE 控制代码在您行走结构时移动每个簇。
    注意 当其他进程写入磁盘时,您可能需要在不同时间更新位图或检索结构,或两者。

有关上述内容的 C# 包装器,请查看此博客文章。

最后,根据您的情况,您可以使用 WMI Defrag Win32_Volume 类上的方法

希望这有帮助。

Please begin with the Wikipedia Article on Disk Fragmentation

Then after that, it depends on how low-level you want to go.

First for the official howto see Defragmenting Files on MSDN.

From the article....

  1. Use the FSCTL_GET_VOLUME_BITMAP control code to find a place on the volume that is large enough to accept an entire file.
    Note If necessary, move other files to make a place that is large enough. Ideally, there is enough unallocated clusters after the first extent of the file that you can move subsequent extents into the space after the first extent.
  2. Use the FSCTL_GET_RETRIEVAL_POINTERS control code to get a map of the current layout of the file on the disk.
  3. Walk the RETRIEVAL_POINTERS_BUFFER structure returned by FSCTL_GET_RETRIEVAL_POINTERS.
  4. Use the FSCTL_MOVE_FILE control code to move each cluster as you walk the structure.
    Note You may need to renew either the bitmap or the retrieval structure, or both at various times as other processes write to the disk.

For a C# wrapper of the above, check out this blog post.

Finally, depending on your situation, you can use the WMI Defrag method on the Win32_Volume class.

Hope this helps.

给不了的爱 2024-08-27 20:35:21

要显示文件系统的碎片状态,您必须找出磁盘的哪些块属于哪些文件。所有不只由连续块组成的文件都是碎片的;它们包含孔和/或块分散在磁盘上。

要对文件系统进行碎片整理,您必须移动块,以便所有文件都是连续的,并重写元数据以使文件系统最终处于一致的状态。

To show the fragmentation state of a filesystem, you would have to find out which blocks of the disk belong to which files. All files that do not solely consist of consecutive blocks are fragmented; they contain holes and/or the blocks are scattered over the disk.

To defragment a filesystem you would have to move around the blocks so that all files are consecutive and rewrite the metadata to have the filesystem in a consistent state in the end.

不气馁 2024-08-27 20:35:21

当文件被保存时,它们使用的字节被放入分配的块中,如果文件增长并且下一个连续块不可用,操作系统开始写入下一个可用块,将文件分成2个片段。

碎片整理通过将块移开(移至可用空间)将文件收集到连续的块中,以便进行碎片整理的文件可以具有连续的块。对于非固态硬盘驱动器,这会影响性能(因为没有寻道时间读取连续块)。

一些碎片整理程序将更常读取的文件移动到磁盘外部(因为距离主轴越远,它旋转得越快)。

When files are saved the bytes they use is put into allocated blocks, if the file grows and the next consecutive block is not available, the OS starts writing to the next available block, splitting the file into 2 fragments.

Defragmentation collects files into consecutive blocks by moving blocks out of the way (into free space) so that the file being defragmented can have consecutive blocks. for non Solid State hard drives this affects performance (as there is no seek time reading consecutive blocks)

Some defragmenters move more commonly read files to the outside of the disk (since it spins faster the further away from the spindle it is).

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