Guest User

Untitled

a guest
Dec 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  2. private static extern SafeFileHandle CreateFile(string lpFileName,[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
  3. [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
  4. IntPtr lpSecurityAttributes,
  5. [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
  6. [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
  7. IntPtr hTemplateFile);
  8.  
  9. SafeFileHandle handle = CreateFile(lpFileName: @"\.C:",dwDesiredAccess: FileAccess.Read,dwShareMode: FileShare.ReadWrite,
  10. lpSecurityAttributes: IntPtr.Zero,dwCreationDisposition: FileMode.OpenOrCreate,
  11. dwFlagsAndAttributes: FileAttributes.Normal,hTemplateFile: IntPtr.Zero);
  12.  
  13. using (FileStream disk = new FileStream(handle, FileAccess.Read))
  14. {
  15. byte[] mbrData = new byte[512];
  16. Console.WriteLine("Starting MBR Backup...");
  17. try
  18. {
  19. disk.Read(mbrData, 0, mbrData.Length);
  20. FileStream mbrSave = new FileStream("mbr.img", FileMode.Create);
  21. mbrSave.Write(mbrData, 0, mbrData.Length);
  22. Console.WriteLine("MBR Backuped to mymbr.img success!");
  23. }
  24. catch (Exception e)
  25. {
  26. Console.WriteLine(e.Message);
  27. }
  28. }
Add Comment
Please, Sign In to add comment