문제

Hi I want to do something when a memory stick attached to PC. Now I use a timer and check it in every tick whether any memory stick is plugged(use DriveInfo or with querying WMI) Is there any event driven model available to do? for example i use an event in my program that raise whenever a memory stick is plugged in to computer?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top