Recent Posts

Sunday, March 16, 2008

Read Write and lock Usb disk Drive Sectors in C#

using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;

namespace PCSUSBCSharp
{
public class PCSUSBCSharp
{

public enum EMoveMethod : uint
{
Begin = 0,
Current = 1,
End = 2
}

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

const uint FILE_SHARE_READ = 0x00000001;
const uint FILE_SHARE_WRITE = 0x00000002;
const uint FILE_SHARE_DELETE = 0x00000004;
const uint OPEN_EXISTING = 3;

const uint GENERIC_READ = (0x80000000);
const uint GENERIC_WRITE = (0x40000000);

const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
const uint FILE_READ_ATTRIBUTES = (0x0080);
const uint FILE_WRITE_ATTRIBUTES = 0x0100;
const uint ERROR_INSUFFICIENT_BUFFER = 122;
const uint FILE_BEGIN=0;


private const Int32 INVALID_HANDLE_VALUE = -1;
//s
private const Int32 FILE_ATTRIBUTE_NORMAL=1;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteFile(IntPtr handle,
byte[] buffer, ushort count, ref ushort written, IntPtr lpOverlapped);


[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadFile(IntPtr handle,
byte[] buffer, ushort toRead, ref ushort read, IntPtr lpOverLapped);

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint SetFilePointer(
IntPtr hFile,
int lDistanceToMove,
int lpDistanceToMoveHigh,
EMoveMethod dwMoveMethod);


[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);


static private IntPtr OpenVolume(string DeviceName)
{
IntPtr hDevice;
hDevice = CreateFile(
@"\\.\" + DeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hDevice == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hDevice;
}


static private IntPtr OpenFile(string path)
{
IntPtr hFile;
hFile = CreateFile(
path,
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
if ((int)hFile == -1)
{
throw new Exception(Marshal.GetLastWin32Error().ToString());
}
return hFile;
}


public static bool b9xME;
public static IntPtr hUSBDisk;


public static bool IsUSBOpen()
{
//return (hUSBDisk?true:false);
return (int)(hUSBDisk)!=-1;
}


private static IntPtr OpenUSBDisk(String fileName)
{
if(IsUSBOpen())
return hUSBDisk;
else
{
IntPtr tmpHandle;
tmpHandle=OpenVolume(fileName);
if((int)tmpHandle==-1)
return new IntPtr(-1);
else
return tmpHandle;
}

}


private static void CloseUSBDisk()
{


if((int)hUSBDisk!=-1)
{

CloseHandle(hUSBDisk);
hUSBDisk=new IntPtr(-1);
}
}




public static void ResetDisk(String fileName)
{

if(IsUSBOpen())
CloseUSBDisk();
hUSBDisk=OpenUSBDisk(fileName);
CloseUSBDisk();

}



public static int WriteUSBDisk(byte []buffer, ushort cylinder, ushort sector,ushort head,String fileName)
{
ushort count=0;
bool retValue;
if(!IsUSBOpen())
hUSBDisk=OpenUSBDisk(fileName);
if((int)hUSBDisk==-1)
return NISH_ERROR;
SetFilePointer(hUSBDisk,
512*(18*(cylinder*2+head)+(sector-1)),0,
EMoveMethod.Begin);
retValue=WriteFile(hUSBDisk,buffer, 512,ref count,IntPtr.Zero);
if(retValue)
return NISH_NO_ERROR;
else
return NISH_ERROR;
}



public static int ReadUSBDisk(byte []buffer, ushort cylinder, ushort sector,ushort head,String fileName)
{
ushort count=1;
bool retValue;
if(!IsUSBOpen())
hUSBDisk=OpenUSBDisk(fileName);
if((int)hUSBDisk==-1)
return NISH_ERROR;
SetFilePointer(hUSBDisk,
512*(18*(cylinder*2+head)+(sector-1)),0,
EMoveMethod.Begin);
retValue=ReadFile(hUSBDisk,buffer,512,ref count,IntPtr.Zero);


if(retValue)
return NISH_NO_ERROR;
else
return NISH_ERROR;

}
static int NISH_NO_ERROR=1;
static int NISH_ERROR=0;


}


}

Related Posts by Categories




4 comments:

Anonymous said...

Can you give an example on how to use this class?

Thanks

rprateek said...

Just copy and paste the code and put it in to a class and use the functions where ever you require.

You need to add the required references in your project.

Then enjoy using it. It is best code for locking the drive.

Thanks Prateek

Anonymous said...

hi.. i'm trying to eject and remount a USB drive through code. Is there any sample code available? or any references? please help, i'm running out of time..

rprateek said...

I haven't done eject or remount of the USB Drive, sorry i wouldn't be able to help you out.

Post a Comment