<mq****@gmail.comwrote in message
news:11**********************@r56g2000hsd.googlegr oups.com...
Is there any way to get raw disk access in C#?
e.g. get the first XX bytes from drive0, partition1 starting from byte
YYY?
(something like the dd command in Unix maybe?)
If I have to use P/Invoke can anyone tell me what libraries are
needed?
Thanks.
You'll have to call Win32 CreateFile using PInvoke, just like you would do using C.
Note also that you need Administrative privileges to access devices.
Following is a small sample to get you started:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern SafeFileHandle CreateFile(string lpFileName, int
dwDesiredAccess, int dwShareMode,
IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint
dwFlagsAndAttributes,
SafeFileHandle hTemplateFile);
internal const int GENERIC_READ = unchecked((int)0x80000000);
internal const int OPEN_EXISTING = 3;
internal const int FILE_ATTRIBUTE_NORMAL = 0x80;
....
const int blockSize = 512;
SafeFileHandle h = null;
h = CreateFile("\\\\.\\PhysicalDrive0",
GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
new SafeFileHandle(IntPtr.Zero, true));
if (! h.IsInvalid ) {
FileStream stream = new FileStream(h, FileAccess.Read);
// Read from stream
else
{
// get error code and throw
int error = Marshal.GetLastWin32Error();
..
}
Willy.