当前上下文中不存在名称为'WM_DEVICECHANGE'的内容。

3

我想要检测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");
                    }
            }

        }

    }
}

添加此功能是为了让您可以提出任何更新建议。 谢谢。


请在此展示您的代码。人们无法读懂您的思维。 - Soner Gönül
@SonerGönül 我刚刚看了你的...我正在编辑。 - Zigma
可能是重复的问题:如何检测USB驱动器是否已插入? - 您忘记从Onsightfree的答案中复制提到的常量。 - Anya Shenanigans
@Petesh 你可以争辩,但事实并非如此。 - Zigma
1个回答

7

您需要声明和定义常量的值:

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

其实我在C#方面还是个初学者,但在C++方面玩得比较开心 :)。这就是为什么让我来检查你的答案。 - Zigma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接