windows 磁盘碎片整理和磁盘检查中的逻辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1085847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logic in Disk Defragmantation & Disk Check
提问by Sauron
What is the logic behind disk defragmentation and Disk Check in Windows? Can I do it using C# coding?
Windows 中的磁盘碎片整理和磁盘检查背后的逻辑是什么?我可以使用 C# 编码吗?
回答by andreialecu
For completeness sake, here's a C# API wrapper for defragmentation:
为了完整起见,这里有一个用于碎片整理的 C# API 包装器:
http://blogs.msdn.com/jeffrey_wall/archive/2004/09/13/229137.aspx
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.
如今,使用这些 API 进行碎片整理(应该是)非常安全。即使您愿意,也不应该破坏文件系统。
Commercial defragmentation programs use the same APIs.
商业碎片整理程序使用相同的 API。
回答by inazaruk
Look at Defragmenting Filesat msdn for possible API helpers.
在 msdn 上查看碎片整理文件以获取可能的 API 帮助程序。
You should carefully think about using C# for this task, as it may introduce some undesired overhead for marshaling into native Win32.
您应该仔细考虑使用 C# 来完成这项任务,因为它可能会引入一些不需要的开销,以便将封送处理到本机 Win32 中。
回答by marklam
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'?
如果您不知道碎片整理的逻辑,并且如果您没有自己编写文件系统所以您无法权威地检查它的错误,为什么不直接启动运行“碎片整理”和“chkdsk”的新进程?
回答by Joey
Mark Russinovich wrote an article Inside Windows NT Disk Defragmentationa 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).
Mark Russinovich不久前写了一篇文章Inside Windows NT Disk Defragmentation,其中提供了深入的细节。如果您真的想这样做,我真的建议您使用内置工具进行碎片整理。更重要的是,在最近的操作系统上,我从来没有看到用户需要关心碎片整理;它会按计划自动完成,并且 MS 的 NTFS 人员在这方面肯定比你更聪明(抱歉,但他们现在已经这样做了一段时间,你没有)。
回答by George Phillips
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.
另一个困难是处理文件系统的复杂性。例如,您最好构建支持 FAT32 而不是 NTFS 的东西,因为前者是一个简单得多的文件系统。
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.
只要您有低级块访问和处理并发问题的一些合理方法(最好通过在不使用的文件系统上工作来处理),您可以在 C#、perl 或任何您喜欢的语言中执行此操作。
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.
但要非常小心。该程序的早期版本会破坏整个文件系统。以后的版本会这样做,但只在不明确的情况下。如果您破坏他们的数据,用户会非常生气和提起诉讼。