Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can overwrite the sectors of an External Hard Drive with the exception of protected files, in my case when trying to overwrite the sectors where MFT is saved.
How to pass any security attributes that will allow me to perform this task.

VB
<pre>
Public Function WriteBytes(ByVal path As String, _
                  ByVal OFFSET_POS As Long, ByVal data As Byte()) As Boolean
           
   Dim handle As IntPtr = IntPtr.Zero
   handle = CreateFile(path, FileAccess.ReadWrite, FileShare.ReadWrite, _
                  IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero)
             
   Dim bytesWritten As UInteger
   Dim nativeOverlapped = New NativeOverlapped With {
                .OffsetLow = 0,
                .OffsetHigh = 0
   }


   Dim msg As Boolean = SetFilePointerEx(handle, OFFSET_POS, IntPtr.Zero, 
                  MoveMethod.FILE_CURRENT)
            
   Debug.WriteLine(String.Format("{0}: Setting Sector to position {1} ", _
   msg, OFFSET_POS \ 512))


   If Not WriteFile(handle, data, CUInt(data.Length), _
                  bytesWritten, nativeOverlapped) Then


        Debug.WriteLine(String.Format("Unable to write to the volume. Error code: {0}", Marshal.GetLastWin32Error()))


        Return False
   End If 
   Return True
End Function


What I have tried:

Performing this Task in Eleveated Mode. Set the offending Partition Access Rights to Full Control for everyone.
Posted
Updated 17-Jul-18 10:35am
v3
Comments
[no name] 17-Jul-18 17:24pm    
I read that Vista and Win7 have restricted overwriting MBR or System Files while Volume is mounted and in use by the File System. I removed the Drive letter, thus unmounted the Partition but I still get Error 5 even when I am reading from PhysicalDrive1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900