The MTP mode device you mentioned, that is, the Media Transfer Protocol, A protocol used to transfer files between computers and mobile devices. Unlike regular USB devices, MTP devices are not typically presented to the operating system as USB Mass Storage and therefore cannot be monitored through standard hot-swap apis.
However, if you are using a Windows operating system, you can try using the WPD API to monitor the access and unplugging of MTP devices. Specifically, you can get a list of currently connected MTP devices using the GetDevices method in the IPortableDeviceManager interface in the WPD API, And use IPortableDeviceEventSystem interface to insert and pull out events of monitoring equipment.
Here is some sample code for your reference:
Imports PortableDeviceApiLib
Public Class Form1
Private WithEvents deviceEventSystem As New PortableDeviceManagerClass()
Private WithEvents deviceEvents As IPortableDeviceEvents
Private connectedDevices As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 创建 PortableDeviceManager 对象
Dim deviceManager As New PortableDeviceManagerClass()
' 获取当前连接的所有设备
Dim devices As PortableDeviceCollection = deviceManager.GetDevices()
' 将设备添加到列表中
For i As Integer = 0 To devices.Count - 1
Dim device As PortableDevice = devices(i)
connectedDevices.Add(device.DeviceId)
listBox1.Items.Add(device.FriendlyName)
Next
' 初始化设备事件系统
deviceEvents = DirectCast(deviceEventSystem, IPortableDeviceEvents)
deviceEvents.AddDeviceList(Nothing)
deviceEvents.DeviceAdded += AddressOf OnDeviceAdded
deviceEvents.DeviceRemoved += AddressOf OnDeviceRemoved
End Sub
Private Sub OnDeviceAdded(ByRef pDeviceId As String) Handles deviceEvents.DeviceAdded
' 设备插入事件处理
If Not connectedDevices.Contains(pDeviceId) Then
connectedDevices.Add(pDeviceId)
Dim device As New PortableDevice()
device.Open(pDeviceId)
listBox1.Items.Add(device.FriendlyName)
device.Close()
End If
End Sub
Private Sub OnDeviceRemoved(ByRef pDeviceId As String) Handles deviceEvents.DeviceRemoved
' 设备拔出事件处理
If connectedDevices.Contains(pDeviceId) Then
connectedDevices.Remove(pDeviceId)
For Each item As Object In listBox1.Items
If item.ToString().Contains(pDeviceId) Then
listBox1.Items.Remove(item)
Exit For
End If
Next
End If
End Sub
End Class
in the sample code above, we by PortableDeviceManagerClass GetDevices method to obtain a list of the current connection all MTP equipment, and add it to the list box. Then, we use IPortableDeviceEventSystem interface to insert and pull out event monitoring equipment, and update the equipment list box in the event handler.