1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public const short FILE_ATTRIBUTE_NORMAL = 0x80;
public const short INVALID_HANDLE_VALUE = -1;
public const uint GENERIC_READ = 0x80000000;
public const uint CREATE_NEW = 1;
public const uint CREATE_ALWAYS = 2;
public const uint OPEN_EXISTING = 3;
public const int FileFlagNoBuffering = 0x20000000;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
var path = string.Format(@"\\.\PHYSICALDRIVE{0}", diskNumber);
using (var handleValue = CreateFile(path, GENERIC_READ, 1, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero))
{
if (handleValue.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
var inBuffer = new byte[512];
using (var disk = new FileStream(handleValue, FileAccess.Read))
{
disk.Seek(0, SeekOrigin.Begin);
for (int i = 0; i < 512; i++)
{
inBuffer[i] = (byte) disk.ReadByte();
}
}
for (int i = 0; i < 512; i++)
{
Console.Write(inBuffer[i].ToString("x2") + " ");
}
Console.Read(); |
Partager