顾锦歌——发现生活的美!

文章 分类 标签
23 11 0

公告

此处是站点公告

C#检测U盘是否插入

顾锦歌 2022-01-13 250 0条评论 C#

首页 / 正文
 public partial class Form1 : Form
{
    #region u盘属性
    public const int WM_DEVICECHANGE = 0x219;//U盘插入后,OS的底层会自动检测到,然后向应用程序发送“硬件设备状态改变“的消息
    public const int DBT_DEVICEARRIVAL = 0x8000;  //就是用来表示U盘可用的。一个设备或媒体已被插入一块,现在可用。
    public const int DBT_CONFIGCHANGECANCELED = 0x0019;  //要求更改当前的配置(或取消停靠码头)已被取消。
    public const int DBT_CONFIGCHANGED = 0x0018;  //当前的配置发生了变化,由于码头或取消固定。
    public const int DBT_CUSTOMEVENT = 0x8006; //自定义的事件发生。 的Windows NT 4.0和Windows 95:此值不支持。
    public const int DBT_DEVICEQUERYREMOVE = 0x8001;  //审批要求删除一个设备或媒体作品。任何应用程序也不能否认这一要求,并取消删除。
    public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;  //请求删除一个设备或媒体片已被取消。
    public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;  //一个设备或媒体片已被删除。
    public const int DBT_DEVICEREMOVEPENDING = 0x8003;  //一个设备或媒体一块即将被删除。不能否认的。
    public const int DBT_DEVICETYPESPECIFIC = 0x8005;  //一个设备特定事件发生。
    public const int DBT_DEVNODES_CHANGED = 0x0007;  //一种设备已被添加到或从系统中删除。
    public const int DBT_QUERYCHANGECONFIG = 0x0017;  //许可是要求改变目前的配置(码头或取消固定)。
    public const int DBT_USERDEFINED = 0xFFFF;  //此消息的含义是用户定义的
    #endregion

    public Form1()
    {
        InitializeComponent();
    }

    #region 检测u盘
    protected override void WndProc(ref Message m)
    {
        try
        {
            if (m.Msg == WM_DEVICECHANGE)
            {
                switch (m.WParam.ToInt32())
                {
                    case WM_DEVICECHANGE:
                        break;
                    case DBT_DEVICEARRIVAL://U盘插入
                        MessageBox.Show("U盘插入");

                        DriveInfo[] s = DriveInfo.GetDrives();

                        foreach (DriveInfo drive in s)

                        {

                            if (drive.DriveType == DriveType.Removable)

                            {

                                label1.Text="U盘已插入,盘符为:" + drive.Name.ToString();

                                break;

                            }

                        }
                        break;
                    case DBT_CONFIGCHANGECANCELED:
                        break;
                    case DBT_CONFIGCHANGED:
                        break;
                    case DBT_CUSTOMEVENT:
                        break;
                    case DBT_DEVICEQUERYREMOVE:
                        break;
                    case DBT_DEVICEQUERYREMOVEFAILED:
                        break;
                    case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                        MessageBox.Show("U盘拔出!");
                        break;
                    case DBT_DEVICEREMOVEPENDING:
                        break;
                    case DBT_DEVICETYPESPECIFIC:
                        break;
                    case DBT_DEVNODES_CHANGED:
                        break;
                    case DBT_QUERYCHANGECONFIG:
                        break;
                    case DBT_USERDEFINED:
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        base.WndProc(ref m);
    }
    #endregion

}

评论(0)

当前没有评论,还不快来留下第一个脚印吧

热门文章

最新评论

  • Jacob Hankinson

    Your go-to source for leads. We can provide business to business and business to consumer leads, custom-tailored to your needs. CustomDatabases.org

  • Otilia Drechsler

    Hi, It is with sad regret to inform you TopDataList.com is shutting down. We are ceasing operations on TopDataList.com and have made our leads available at a $149 once off fee. Visit us on TopDataList.com Regards, Otilia

  • Ervin Quinn

    Hello, from CustomData.click we are a provider of unique databases that could help your business. Please visit us at CustomData.click to see if we can help you. Regards, Ervin

  • Keri Cardus

    Hello. It is with sad regret to inform you TopDataList.com is shutting down. We have made all our databases available for you for a once off fee. Visit us on TopDataList.com

  • Beth Steele

    ZippyLeads.org is running an easter special till the 18th of April. Get all the leads you need for your company with our easter special.

  • Johan Fourie

    Hello. My name is Johan Fourie and I am looking to sell DataList.biz. We are a data company that has been in the industry for 12 years. We do around $170k/year in revenue. 1) I am looking to sell 50% of the business for $5k. 2) It would be helpful if you are knowledgeable about the Data Business. 3) I am looking for someone that is willing to take over administration, support, client relations. 4) I will continue to do the marketing for new products. 5) You will accept all future income and pay me from it. Please contact me on WhatsApp +27 72 280 1952 or my personal email: johanfourieinc@gmail.com if you are interested in this and we can have a call. Regards, Johan Fourie

  • Pam Using

    Hi, I am interested in some of your products. Please give me a call on +1 304-873-4360

  • Typecho

    欢迎加入 Typecho 大家族

日历

2025年01月

   1234
567891011
12131415161718
19202122232425
262728293031 

标签云

文章目录

推荐关键字:

上一张 下一张