tags:

views:

55

answers:

2

I would like to write an application that will create an 'image' of a flash drive. This includes the total topography of the drive, not just the files. So if the drive is 4GB you get a 4GB file. Is this possible, and if so, could someone point me in the direction of information on how this may be accomplished?

A: 

Have you tried simply opening the drive as a file and copying it?

Loren Pechtel
That would not give him the image. From what he's describing, he wants a full "sector copy" of the flash drive.
Robaticus
@Robaticus What would be missing?
Loren Pechtel
@Loren - Bootsector information, non-native filesystems.
Robaticus
@Robaticus - Why would that be missing? I'm saying to open the *DRIVE* as a file. Windows will let you.
Loren Pechtel
I wasn't aware that would work for sector copying. Maybe if you have some code that will work for him.
Robaticus
A: 

It is possible. I did it for an internal app, so I can't just paste the source for it, but I can give you some hints. You will have to P/Invoke some things.

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "CreateFileW", SetLastError = true)]
public static extern IntPtr CreateFile(string name, int access, int share, byte[] attributes, int create, int flags, IntPtr template);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern int CloseHandle(IntPtr handle);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern int DeviceIoControl(IntPtr handle, DiskIoctl ioctl, byte[] inBuffer, int inBufferSize, byte[] outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetLogicalDriveStringsW", SetLastError = true)]
public static extern int GetLogicalDriveStrings(int bufferLength, byte[] buffer);

public enum DiskIoctl
{
    ScsiPassThrough = 315396,

    Lock = 589848,

    Unlock = 589852,

    Dismount = 589856,

    UpdateProperties = 459072,

    GetDiskLayout = 475148,

    SetDiskLayout = 507920
}

public enum ScsiOp
{
    ReadCapacity = 0x25,

    Read = 0x28,

    Write = 0x2A
}
Bryan