Results 1 to 28 of 28

Thread: [RESOLVED] USB Storage device

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: USB Storage device

    i already searched in the web almost a day but still i can't find any solution
    *****************
    VB6,PHP,VS 2005

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: USB Storage device

    1/2
    Code:
    using DetectDrive;
    using System.IO;
    
    namespace SimpleDetector
    {
        public partial class Form1 : Form
        {
            private DriveDetector driveDetector = null;
    
            public Form1()
            {
                InitializeComponent();
                driveDetector = new DriveDetector(this);
                driveDetector.DeviceArrived += new DriveDetectorEventHandler(OnDriveArrived);
                driveDetector.DeviceRemoved += new DriveDetectorEventHandler(OnDriveRemoved);
                driveDetector.QueryRemove += new DriveDetectorEventHandler(OnQueryRemove);
            }
    
            // Called by DriveDetector when removable device in inserted 
            private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
            {
                // Report the event in the listbox.
                // e.Drive is the drive letter for the device which just arrived, e.g. "E:\\"
                listBox1.Items.Clear();
                string s = "Drive arrived " + e.Drive;
                listBox1.Items.Add(s);
                MessageBox.Show(e.Drive.ToString());
    
                // If you want to be notified when drive is being removed (and be able to cancel it), 
                // set HookQueryRemove to true 
                if ( checkBoxAskMe.Checked )   
                    e.HookQueryRemove = true;                 
            }
    
            // Called by DriveDetector after removable device has been unpluged 
            private void OnDriveRemoved(object sender, DriveDetectorEventArgs e)
            {
                // TODO: do clean up here, etc. Letter of the removed drive is in e.Drive;
                
                // Just add report to the listbox
                listBox1.Items.Clear();
                string s = "Drive removed " + e.Drive;
                listBox1.Items.Add(s);
            }
    
            // Called by DriveDetector when removable drive is about to be removed
            private void OnQueryRemove(object sender, DriveDetectorEventArgs e)
            {
                // Should we allow the drive to be unplugged?
                if (checkBoxAskMe.Checked)
                {
                    if (MessageBox.Show("Allow remove?", "Query remove",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        e.Cancel = false;       // Allow removal
                    else
                        e.Cancel = true;        // Cancel the removal of the device  
                }
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);        // call default p
                
                if (driveDetector != null)
                {
                    driveDetector.WndProc(ref m);
                }
            }
    
            // User checked the "Ask me before drive can be disconnected box"        
            private void checkBoxAskMe_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBoxAskMe.Checked)
                {
                    // Is QueryRemove enabled? 
                    // If not, we will enable it for the drive which is selected 
                    // in the listbox. 
                    // If the listbox is empty, no drive has been detected yet so do nothing now.
                    if (!driveDetector.IsQueryHooked && listBox1.Items.Count > 0)
                    {
                        if (listBox1.SelectedItem == null)
                        {
                            MessageBox.Show("Please choose the drive for which you wish to be asked in the list (select its message).");
                            checkBoxAskMe.Checked = false;
                            return;
                        }
    
                        bool ok = false;
                        string s = (string)listBox1.SelectedItem;
                        int n = s.IndexOf(':');
                        if (n > 0)
                        {
                            s = s.Substring(n - 1, 3);  // Gets drive letter from the message, (e.g. "E:\\") 
    
                            // Tell DriveDetector to monitor this drive
                            ok = driveDetector.EnableQueryRemove(s);
                        }
    
                        if (!ok)
                            MessageBox.Show("Sorry, for some reason notification for QueryRemove did not work out.");
    
                    }
    
                }
                else
                {
                    if (driveDetector.IsQueryHooked)
                        driveDetector.DisableQueryRemove();
                }                        
            }
    *****************
    VB6,PHP,VS 2005

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: USB Storage device

    2/2
    [CODE]
    using System.Windows.Forms; // required for Message
    using System.Runtime.InteropServices; // required for Marshal
    using System.IO;
    using Microsoft.Win32.SafeHandles;

    namespace DetectDrive
    {

    // Delegate for event handler to handle the device events
    public delegate void DriveDetectorEventHandler(Object sender, DriveDetectorEventArgs e);

    public class DriveDetectorEventArgs : EventArgs
    {
    public DriveDetectorEventArgs()
    {
    Cancel = false;
    Drive = "";
    HookQueryRemove = false;
    }
    public bool Cancel;

    public string Drive;
    public bool HookQueryRemove;

    }

    class DriveDetector : IDisposable
    {

    public event DriveDetectorEventHandler DeviceArrived;
    public event DriveDetectorEventHandler DeviceRemoved;
    public event DriveDetectorEventHandler QueryRemove;

    public DriveDetector(Control control)
    {
    mFileToOpen = null;
    mDeviceNotifyHandle = IntPtr.Zero;
    mRecipientHandle = control.Handle;
    mCurrentDrive = "";
    }
    public DriveDetector(Control control, string FileToOpen)
    {
    mFileToOpen = FileToOpen;
    mDeviceNotifyHandle = IntPtr.Zero;
    mRecipientHandle = control.Handle;
    mCurrentDrive = "";
    }
    public bool IsQueryHooked
    {
    get
    {
    if (mDeviceNotifyHandle == IntPtr.Zero)
    return false;
    else
    return true;
    }
    }

    public string HookedDrive
    {
    get
    {
    return mCurrentDrive;
    }
    }

    public FileStream OpenedFile
    {
    get
    {
    return mFileOnFlash;
    }
    }
    public bool EnableQueryRemove(string fileOnDrive)
    {
    if (fileOnDrive == null || fileOnDrive.Length == 0)
    throw new ArgumentException("Drive path must be supplied to register for Query remove.");
    if ( fileOnDrive.Length == 2 && fileOnDrive[1] == ':' )
    fileOnDrive += '\\'; // append "\\" if only drive letter with ":" was passed in.

    if (mDeviceNotifyHandle != IntPtr.Zero)
    {
    RegisterForDeviceChange(false, null);
    }

    if (!File.Exists(fileOnDrive))
    mFileToOpen = null; // use any file
    else
    mFileToOpen = fileOnDrive;

    RegisterQuery(Path.GetPathRoot(fileOnDrive));
    if (mDeviceNotifyHandle == IntPtr.Zero)
    return false; // failed to register

    return true;
    }

    public void DisableQueryRemove()
    {
    if (mDeviceNotifyHandle != IntPtr.Zero)
    {
    RegisterForDeviceChange(false, null);
    }
    }

    public void Dispose()
    {
    RegisterForDeviceChange(false, null);
    }

    #region WindowProc
    public void WndProc(ref Message m)
    {
    int devType;
    char c;

    if (m.Msg == WM_DEVICECHANGE)
    {
    // WM_DEVICECHANGE can have several meanings depending on the WParam value...
    switch (m.WParam.ToInt32())
    {

    //
    // New device has just arrived
    //
    case DBT_DEVICEARRIVAL:

    devType = Marshal.ReadInt32(m.LParam, 4);
    if (devType == DBT_DEVTYP_VOLUME)
    {
    DEV_BROADCAST_VOLUME vol;
    vol = (DEV_BROADCAST_VOLUME)
    Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));

    // Get the drive letter
    c = DriveMaskToLetter(vol.dbcv_unitmask);

    if ( tempDeviceArrived != null )
    {
    DriveDetectorEventArgs e = new DriveDetectorEventArgs();
    e.Drive = c + ":\\";
    tempDeviceArrived(this, e);

    // Register for query remove if requested
    if (e.HookQueryRemove)
    {
    // If something is already hooked, unhook it now
    if (mDeviceNotifyHandle != IntPtr.Zero)
    {
    RegisterForDeviceChange(false, null);
    }

    RegisterQuery(c + ":\\");
    }
    } // if has event handler


    }
    break;
    case DBT_DEVICEQUERYREMOVE:

    devType = Marshal.ReadInt32(m.LParam, 4);
    if (devType == DBT_DEVTYP_HANDLE)
    {
    DriveDetectorEventHandler tempQuery = QueryRemove;
    if (tempQuery != null)
    {
    DriveDetectorEventArgs e = new DriveDetectorEventArgs();
    e.Drive = mCurrentDrive; // drive which is hooked
    tempQuery(this, e);

    // If the client wants to cancel, let Windows know
    if (e.Cancel)
    {
    m.Result = (IntPtr)BROADCAST_QUERY_DENY;
    }
    else
    {
    // Close the handle so that the drive can be unmounted
    if (mFileOnFlash != null)
    {
    mFileOnFlash.Close();
    mFileOnFlash = null;
    }
    }

    }
    }
    break;


    //
    // Device has been removed
    //
    case DBT_DEVICEREMOVECOMPLETE:

    devType = Marshal.ReadInt32(m.LParam, 4);
    if (devType == DBT_DEVTYP_VOLUME)
    {
    devType = Marshal.ReadInt32(m.LParam, 4);
    if (devType == DBT_DEVTYP_VOLUME)
    {
    DEV_BROADCAST_VOLUME vol;
    vol = (DEV_BROADCAST_VOLUME)
    Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
    c = DriveMaskToLetter(vol.dbcv_unitmask);

    DriveDetectorEventHandler tempDeviceRemoved = DeviceRemoved;
    if (tempDeviceRemoved != null)
    {
    DriveDetectorEventArgs e = new DriveDetectorEventArgs();
    e.Drive = c + ":\\";
    tempDeviceRemoved(this, e);
    }

    }
    }
    break;
    }

    }

    }
    #endregion
    *****************
    VB6,PHP,VS 2005

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: USB Storage device

    #region Private Area

    private FileStream mFileOnFlash = null;

    private string mFileToOpen;
    private IntPtr mDeviceNotifyHandle;

    private IntPtr mRecipientHandle;
    private string mCurrentDrive;


    // Win32 constants
    private const int DBT_DEVTYP_DEVICEINTERFACE = 5;
    private const int DBT_DEVTYP_HANDLE = 6;
    private const int BROADCAST_QUERY_DENY = 0x424D5144;
    private const int WM_DEVICECHANGE = 0x0219;
    private const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
    private const int DBT_DEVICEQUERYREMOVE = 0x8001; // Preparing to remove (any program can disable the removal)
    private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // removed
    private const int DBT_DEVTYP_VOLUME = 0x00000002; // drive type is logical volume

    private void RegisterQuery(string drive)
    {
    bool register = true;

    if (mFileToOpen == null)
    {
    // If client gave us no file, let's pick one on the drive...
    mFileToOpen = GetAnyFile(drive);
    if (mFileToOpen.Length == 0)
    return; // no file found on the flash drive
    }
    else
    {

    if (mFileToOpen.Contains(":"))
    {
    string tmp = mFileToOpen.Substring(3);
    string root = Path.GetPathRoot(drive);
    mFileToOpen = Path.Combine(root, tmp);
    }
    else
    mFileToOpen = Path.Combine(drive, mFileToOpen);
    }


    try
    {
    mFileOnFlash = new FileStream(mFileToOpen, FileMode.Open);
    }
    catch (Exception)
    {
    // just do not register if the file could not be opened
    register = false;
    }

    if (register)
    {
    RegisterForDeviceChange(true, mFileOnFlash.SafeFileHandle);
    mCurrentDrive = drive;
    }
    }

    private void RegisterForDeviceChange(bool register, SafeFileHandle fileHandle)
    {
    if (register)
    {
    DEV_BROADCAST_HANDLE data = new DEV_BROADCAST_HANDLE();
    data.dbch_devicetype = DBT_DEVTYP_HANDLE;
    data.dbch_reserved = 0;
    data.dbch_nameoffset = 0;
    data.dbch_handle = fileHandle.DangerousGetHandle(); //Marshal. fileHandle;
    data.dbch_hdevnotify = (IntPtr)0;
    int size = Marshal.SizeOf(data);
    data.dbch_size = size;
    IntPtr buffer = Marshal.AllocHGlobal(size);
    Marshal.StructureToPtr(data, buffer, true);

    mDeviceNotifyHandle = Native.RegisterDeviceNotification(mRecipientHandle, buffer, 0);
    }
    else
    {
    if (mDeviceNotifyHandle != IntPtr.Zero)
    Native.UnregisterDeviceNotification(mDeviceNotifyHandle);
    mDeviceNotifyHandle = IntPtr.Zero;
    mCurrentDrive = "";
    if (mFileOnFlash != null)
    {
    mFileOnFlash.Close();
    mFileOnFlash = null;
    }
    }

    }

    private static char DriveMaskToLetter(int mask)
    {
    char letter;
    string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // 1 = A
    // 2 = B
    // 4 = C...
    int cnt = 0;
    int pom = mask / 2;
    while (pom != 0)
    {
    // while there is any bit set in the mask
    // shift it to the righ...
    pom = pom / 2;
    cnt++;
    }

    if (cnt < drives.Length)
    letter = drives[cnt];
    else
    letter = '?';

    return letter;
    }

    private string GetAnyFile(string drive)
    {
    string file = "";
    // First try files in the root
    string[] files = Directory.GetFiles(drive);
    if (files.Length == 0)
    {
    // if no file in the root, search whole drive
    files = Directory.GetFiles(drive, "*.*", SearchOption.AllDirectories);
    }

    if (files.Length > 0)
    file = files[0]; // get the first file

    // return empty string if no file found
    return file;
    }
    #endregion


    #region Native Win32 API
    private class Native
    {
    // HDEVNOTIFY RegisterDeviceNotification(HANDLE hRecipient,LPVOID NotificationFilter,DWORD Flags);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, uint Flags);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern uint UnregisterDeviceNotification(IntPtr hHandle);

    }

    // Structure with information for RegisterDeviceNotification.
    [StructLayout(LayoutKind.Sequential)]
    public struct DEV_BROADCAST_HANDLE
    {
    public int dbch_size;
    public int dbch_devicetype;
    public int dbch_reserved;
    public IntPtr dbch_handle;
    public IntPtr dbch_hdevnotify;
    public Guid dbch_eventguid;
    public long dbch_nameoffset;
    //public byte[] dbch_data[1]; // = new byte[1];
    public byte dbch_data;
    public byte dbch_data1;
    }

    // Struct for parameters of the WM_DEVICECHANGE message
    [StructLayout(LayoutKind.Sequential)]
    public struct DEV_BROADCAST_VOLUME
    {
    public int dbcv_size;
    public int dbcv_devicetype;
    public int dbcv_reserved;
    public int dbcv_unitmask;
    }
    #endregion

    }
    }
    [/CODE]

    before my head breaks...
    i think i found what im lookin for, my next step now is how could i search all files including subfolders (considering i don't know the folder names)in the USB storage since it is obvious that i already know the drive letter?

    below is helpful but you must specify the folder names
    foreach (string filepath in System.IO.Directory.GetFiles(@"C:\folder" , "*.txt", System.IO.SearchOption.AllDirectories))


    sorry for many texts i just wanna share the code for future reference.
    *****************
    VB6,PHP,VS 2005

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: USB Storage device

    below is helpful but you must specify the folder names
    foreach (string filepath in System.IO.Directory.GetFiles(@"C:\folder" , "*.txt", System.IO.SearchOption.AllDirectories))
    *****************
    VB6,PHP,VS 2005

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width