如何读取位整个磁盘位的内容 [英] How to read the contents of an entire disk bit by bit
问题描述
我有一个闪存卡,我需要计算的驱动器的全部内容的校验。
I've got a flash card that I need to compute a checksum on the entire contents of the drive.
如果我能获得一个流整个驱动器我只是有点看它一下。
If I could acquire a stream to the entire drive I could just read it in bit by bit.
有谁知道是否有这个做什么?
Does anyone know if there is an API for doing this?
我看到的一切到目前为止,要求我打开一个文件。
Everything I see so far requires me to open a file.
有什么办法,只是读取位整个驱动器的内容一点?
Is there any way to just read an entire drive's contents bit by bit?
推荐答案
如果你想编写C#代码,那么你就必须使用P / Invoke来从磁盘(RAW访问)读取数据。
If you want to write C# code, then you'll have to use P/Invoke to read data from your disk (RAW access).
有什么办法,只是读取位整个驱动器的内容一点?
Is there any way to just read an entire drive's contents bit by bit?
您将不得不作出在驱动器之间的差异(逻辑您的闪存卡的表示,一个安装了一个文件系统,通过指定的驱动器盘符),并在磁盘(物理您的闪存卡的代表性,由磁盘号指定)。
You'll have to make a difference between the drive (logical representation of your flash card, with a FileSystem installed on it, specified by the drive letter) and the disk (physical representation of your flash card, specified by the disk number).
请参阅我以前的答案有关如何读取原始数据驱动器/盘:
See my previous answer about how to read RAW data from a drive/disk:
基本上,你首先需要一个句柄到磁盘/驱动器:
Basically, you'll first need a handle to the disk/drive:
// For a DISK:
IntPtr hDisk = CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", diskNumber),
GenericRead,
Read | Write,
0,
OpenExisting,
0,
IntPtr.Zero);
// For a DRIVE
IntPtr hDrive = NativeMethods.CreateFile(
string.Format("\\\\.\\{0}:", DriveLetter)
GenericRead,
Read | Write,
IntPtr.Zero,
OpenExisting,
0,
IntPtr.Zero);
然后使用 SetFilePointerEx
(这样你就可以移动偏移要读),的ReadFile
(填入从磁盘/驱动器读取的字节)的缓冲区, CloseHandle的
(关闭通过的CreateFile打开手柄)。
Then use SetFilePointerEx
(so you can move the offset where you want to read), ReadFile
(fills a buffer with bytes read from the disk/drive), CloseHandle
(closes the handle opened by CreateFile).
读取块磁盘/驱动器(所以基本上,从偏移量为0的循环,以抵消磁盘/驱动器大小。)
Read the disk/drive by chunks (so basically, a loop from offset "0" to offset "disk/drive size").
什么是重要的(或的ReadFile
总是会失败):读取块的尺寸必须是你的磁盘(512字节通常情况下)。
What's important (or ReadFile
will always fail): the size of read chunks must be a multiple of the sector size of your disk (512 bytes generally).
这篇关于如何读取位整个磁盘位的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!