gpt4 book ai didi

c# - 当前上下文中不存在名称 'WM_DEVICECHANGE'

转载 作者:行者123 更新时间:2023-11-30 15:00:04 27 4
gpt4 key购买 nike

我正在尝试检测 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");
}
}

}

}
}

已添加,以便您可以在其中提出任何更新建议。谢谢

最佳答案

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

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

关于c# - 当前上下文中不存在名称 'WM_DEVICECHANGE',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com