How to detect a USB drive has been plugged in?

梦想与她 提交于 2019-11-26 04:11:34

问题


I want to build a program that detects if a usb (or two or more) are plugged in (and copy all contents to any folder on a hard disk)

Any ideas? I have this,

using System.Runtime.InteropServices;

But it is not the easy way (that I believe). I want something easy.

I have another idea (if (folder exist) then copy) something -- but there may be a problem with that, and I want a good solution.

There may also be a tool called SerialPort; can I use it? If so, how do I use it?


回答1:


It is easy to check for removable devices. However, there's no guarantee that it is a USB device:

var drives = DriveInfo.GetDrives()
    .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);

This will return a list of all removable devices that are currently accessible. More information:

  • The DriveInfo class (msdn documentation)
  • The DriveType enumeration (msdn documentation)



回答2:


Detecting if a USB drive is inserted is not simple. However, it is definitely possible and the work has been done for you. Here are two links that each detail an answer on how to do this in C#:

http://www.codeproject.com/KB/system/DriveDetector.aspx

http://www.codeproject.com/Messages/2126647/Re-Csharp-USB-Detection.aspx

Either solution should work fine for you. You can adapt them to fit your needs.

Edit: Here is a third solution in case you wanted more options (basically they are all the same, but choice is good, right?):

http://tempuzfugit.wordpress.com/2007/10/08/external-storage-unit-detection-with-c-in-net-usb-card-readers-etc/




回答3:


Here is a code that works for me, which is a part from the website above combined with my early trials: http://www.codeproject.com/KB/system/DriveDetector.aspx

This basically makes your form listen to windows messages, filters for usb drives and (cd-dvds), grabs the lparam structure of the message and extracts the drive letter.

protected override void WndProc(ref Message m)
    {

        if (m.Msg == WM_DEVICECHANGE)
        {
            DEV_BROADCAST_VOLUME vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
            if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL) &&  (vol.dbcv_devicetype == DBT_DEVTYPVOLUME) )
            {
                MessageBox.Show(DriveMaskToLetter(vol.dbcv_unitmask).ToString());
            }
            if ((m.WParam.ToInt32() == DBT_DEVICEREMOVALCOMPLETE) && (vol.dbcv_devicetype == DBT_DEVTYPVOLUME))
            {
                MessageBox.Show("usb out");
            }
        }
        base.WndProc(ref m);
    }

    [StructLayout(LayoutKind.Sequential)] //Same layout in mem
    public struct DEV_BROADCAST_VOLUME
    {
        public int dbcv_size;
        public int dbcv_devicetype;
        public int dbcv_reserved;
        public int dbcv_unitmask;
    }

    private static char DriveMaskToLetter(int mask)
    {
        char letter;
        string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //1 = A, 2 = B, 3 = C
        int cnt = 0;
        int pom = mask / 2;
        while (pom != 0)    // while there is any bit set in the mask shift it right        
        {        
            pom = pom / 2;
            cnt++;
        }
        if (cnt < drives.Length)
            letter = drives[cnt];
        else
            letter = '?';
        return letter;
    }

Do not forget to add this:

using System.Runtime.InteropServices;

and the following constants:

    const int WM_DEVICECHANGE = 0x0219; //see msdn site
    const int DBT_DEVICEARRIVAL = 0x8000;
    const int DBT_DEVICEREMOVALCOMPLETE = 0x8004;
    const int DBT_DEVTYPVOLUME = 0x00000002;  



回答4:


Microsoft API Code Pack. ShellObjectWatcher class.



来源:https://stackoverflow.com/questions/6003822/how-to-detect-a-usb-drive-has-been-plugged-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!