名称“WM_DEVICECHANGE”不在当前情况下存在(The name 'WM_DEVI

2019-08-20 07:23发布

我想检测USB到达事件。 我试图重写wndproc()用于获取我的消息。 但我面临着Windows消息的错误。

错误是:

The name 'WM_DEVICECHANGE' does not exist in the current context

The name 'DBT_DEVICEARRIVAL' does not exist in the current context

另外这是我试过的代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;         
using System.IO;
using Microsoft.Win32.SafeHandles; 

namespace USBCheckerApp
{
    public partial class Form1 : Form
    {
        bool bDeviceFound = false;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!bDeviceFound)
            {
                button1.Enabled = false;
            }


        }
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_DEVICECHANGE:
                    if (m.WParam == DBT_DEVICEARRIVAL)
                    {
                        MessageBox.Show("MEDIA FOUND");
                    }
            }

        }

    }
}

新增所以,你可以建议在同任何updations。 谢谢

Answer 1:

您必须声明和定义常量的值:

private const int DBT_DEVICEARRIVAL = 0x8000;
private const int WM_DEVICECHANGE = 0x0219;


文章来源: The name 'WM_DEVICECHANGE' does not exist in the current context