1

嗨,当记忆棒连接到 PC 时,我想做一些事情。现在我使用计时器并在每个滴答声中检查是否插入了任何记忆棒(使用 DriveInfo 或查询 WMI) 是否有可用的事件驱动模型?例如,我在程序中使用了一个事件,每当将记忆棒插入计算机时就会引发该事件?

4

1 回答 1

1

You want to handle the WM_DEVICECHANGE message in your wndproc. When you handle that, you can also call RegisterDeviceNotification to get notification that the stick is being cleanly ejected.

When you recieve a WM_DEVICECHANGE, you want to check the wParam - DBT_DEVICEARRIVAL (0x800) is what you're looking for.

In C++, you should have no trouble with the wndproc. In c#, you'll want to override your main form's WndProc method and check m.Msg == 0x219 and m.WParam == 0x800. Note that not ALL of these will be a memory stick - but you can use this to notify you to check for drive insertion rather than a timer.

于 2010-10-05T12:43:27.073 回答