当您潜伏在 MFT 中时,可以采用两种简单的方法来打开文件 - 您可以调用按ID打开文件使用该文件参考号(Vista 及更高版本),或者您可以通过遍历读取 MFT 时构建的列表,然后调用创建文件与组装的名称。
您想要将 CreateFile 或 OpenFileByID 中的句柄获取到 SafeFileHandle 中:
[DllImport( "kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
internal static extern SafeFileHandle CreateFile( string lpFileName, EFileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile );
[DllImport( "kernel32.dll", SetLastError = true )]
internal static extern SafeFileHandle OpenFileById( IntPtr volumeHandle, ref FileIdDescriptor lpFileId, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwFlagsAndAttributes );
一旦您拥有 SafeFileHandle(并且您已检查它是否有效),您就可以将其传递给 FileStream 构造函数并像平常一样读/写文件。
每个文件都在 MFT 中表示,但有一些注意事项。例如,单个文件可以位于文件层次结构中的多个位置,但所有文件都有一个 MFT 条目 - 这些是所谓的硬链接(它们不是副本 - 有多个入口点一个文件 - 令人头疼的事情比比皆是)。有数千个这样的。有一些 API 可以用来查询硬链接,但它变得很难看。