磁盘碎片整理中的逻辑 磁盘检查

发布于 2024-07-25 18:24:42 字数 55 浏览 5 评论 0原文

Windows 中的磁盘碎片整理和磁盘检查背后的逻辑是什么? 我可以使用 C# 编码来完成吗?

What is the logic behind disk defragmentation and Disk Check in Windows? Can I do it using C# coding?

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

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

发布评论

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

评论(5

风柔一江水 2024-08-01 18:24:42

为了完整起见,这里有一个用于碎片整理的 C# API 包装器:

http:// /blogs.msdn.com/jeffrey_wall/archive/2004/09/13/229137.aspx

如今,使用这些 API 进行碎片整理(应该是)非常安全。 即使您愿意,也不应该损坏文件系统。

商业碎片整理程序使用相同的 API。

For completeness sake, here's a C# API wrapper for defragmentation:

http://blogs.msdn.com/jeffrey_wall/archive/2004/09/13/229137.aspx

Defragmentation with these APIs is (supposed to be) very safe nowadays. You shouldn't be able to corrupt the file system even if you wanted to.

Commercial defragmentation programs use the same APIs.

我还不会笑 2024-08-01 18:24:42

请参阅 msdn 上的对文件进行碎片整理以了解可能的信息API 助手。

您应该仔细考虑使用 C# 来完成此任务,因为它可能会在封送到本机 Win32 时引入一些不需要的开销。

Look at Defragmenting Files at msdn for possible API helpers.

You should carefully think about using C# for this task, as it may introduce some undesired overhead for marshaling into native Win32.

没有伤那来痛 2024-08-01 18:24:42

如果您不知道碎片整理的逻辑,并且文件系统不是您自己编写的,因此您无法权威地检查它是否有错误,为什么不启动运行“defrag”和“chkdsk”的新进程呢?

If you don't know the logic for defragmentation, and if you didn't write the file system yourself so you can't authoritatively check it for errors, why not just start new processes running 'defrag' and 'chkdsk'?

塔塔猫 2024-08-01 18:24:42

Mark Russinovich 不久前写了一篇文章Inside Windows NT Disk Defragmentation,其中提供深入的细节。 如果您真的想这样做,我真的建议您使用内置的碎片整理工具。 更重要的是,在最近的操作系统上,我从未见过用户需要关心碎片整理; 它将按计划自动完成,MS 的 NTFS 人员在这方面肯定比您更聪明(抱歉,但他们这样做已经有一段时间了,而您却没有)。

Mark Russinovich wrote an article Inside Windows NT Disk Defragmentation a while ago which gives in-depth details. If you really want to do this I would really advise you to use the built-in facilities for defragmenting. More so, on recent OSes I have never seen a need as a user to even care about defragmenting; it will be done automatically on a schedule and the NTFS folks at MS are definitely smarter at that stuff than you (sorry, but they do this for some time now, you don't).

美人如玉 2024-08-01 18:24:42

尽管文件系统很重要,但它只不过是一种将文件名映射到磁盘块列表的数据结构。 并跟踪元信息,例如文件的实际长度和保存文件列表(例如目录)的特殊文件。 磁盘检查器验证数据结构是否一致。 也就是说,每个磁盘块必须可以自由分配给文件或属于单个文件。 它还可以检查某些情况,其中一组磁盘块似乎是应该位于目录中但由于某种原因而不是在目录中的文件。

碎片整理是指查看分配给每个文件的磁盘块列表。 如果文件使用一组连续的块而不是分散在整个磁盘上的块,那么文件通常加载速度会更快。 通常,如果使用中的所有磁盘块都将自身限制在磁盘的单个连续范围内,则整个文件系统的性能将最佳。 因此,诀窍是安全地移动磁盘块以实现此目的,同时不破坏文件系统。

这里的主要困难是在使用磁盘时运行这些应用程序。 这是可能的,但必须非常非常小心,不要犯某种明显或极其微妙的错误并破坏大部分或全部文件。 离线处理文件系统更容易。

另一个困难是处理文件系统的复杂性。 例如,您最好构建支持 FAT32 而不是 NTFS 的系统,因为前者是一个非常非常简单的文件系统。

只要您具有低级块访问和一些处理并发问题的合理方法(最好通过在不使用文件系统时对其进行处理来处理),您就可以使用 C#、perl 或您喜欢的任何语言来执行此操作。

但要非常小心。该程序的早期版本会破坏整个文件系统。 以后的版本会这样做,但只有在不明确的情况下才会这样做。 如果你破坏用户的数据,用户会非常愤怒并提起诉讼。

Despite its importance, the file system is no more than a data structure that maps file names into lists of disk blocks. And keeps track of meta-information such as the actual length of the file and special files that keep lists of files (e.g., directories). A disk checker verifies that the data structure is consistent. That is, every disk block must either be free for allocation to a file or belong to a single file. It can also check for certain cases where a set of disk blocks appears to be a file that should be in a directory but is not for some reason.

Defragmentation is about looking at the lists of disk blocks assigned to each file. Files will generally load faster if they use a contiguous set of blocks rather than ones scattered all over the disk. And generally the entire file system will perform best if all the disk blocks in use confine themselves to a single congtiguous range of the disk. Thus the trick is moving disk blocks around safely to achieve this end while not destroying the file system.

The major difficulty here is running these application while a disk is in use. It is possible but one has to be very, very, very careful not to make some kind of obvious or extremely subtle error and destroy most or all of the files. It is easier to work on a file system offline.

The other difficulty is dealing with the complexities of the file system. For example, you'd be much better off building something that supports FAT32 rather than NTFS because the former is a much, much simpler file system.

As long as you have low-level block access and some sensible way for dealing with concurrency problems (best handled by working on the file system when it is not in use) you can do this in C#, perl or any language you like.

BUT BE VERY CAREFUL. Early versions of the program will destroy entire file systems. Later versions will do so but only under obscure circumstances. And users get extremely angry and litigious if you destroy their data.

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