Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using Microsoft.Win32.SafeHandles;
- namespace ConsoleApplication1
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- long position = 0;
- const int bWidth = 16;
- SafeFileHandle handle = null;
- try
- {
- handle = CreateFile("\\\\.\\PhysicalDrive0", FileAccess.Read, FileShare.Write | FileShare.Read | FileShare.Delete, IntPtr.Zero, FileMode.Open, FileAttributes.System, IntPtr.Zero);
- if (handle.IsInvalid)
- {
- Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
- }
- using (var fs = new FileStream(handle, FileAccess.Read))
- {
- using (var br = new BinaryReader(fs, Encoding.UTF8))
- {
- while (fs.Position < 512)
- {
- Console.Write($"{position:X8} ");
- var bytes = br.ReadBytes(bWidth);
- foreach (var b in bytes)
- {
- Console.Write($"{b:X2} ");
- }
- Console.WriteLine($" {Encoding.UTF8.GetString(bytes)}");
- position = fs.Position;
- }
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- finally
- {
- handle?.Close();
- }
- Console.WriteLine("Press any key to close.");
- Console.ReadKey();
- }
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- static extern SafeFileHandle CreateFile(
- string lpFileName,
- [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
- [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
- IntPtr lpSecurityAttributes,
- [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
- [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
- IntPtr hTemplateFile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment