none
使用DriveInfo这个类获取磁盘盘符,用listview展示磁盘信息,假若有移动设备,且用户突然扒开移动存储设备,如何监控?或者用户点击移动设备的磁盘时,如何提示用户? RRS feed

  • 问题

  • 使用DriveInfo这个类获取磁盘盘符,用listview展示磁盘信息,假若有移动设备,且用户突然扒开移动存储设备,也就是拔掉u盘和移动硬盘之类的,如何监控?或者用户点击拔掉的移动磁盘对应的listviewitem时,如何提示用户“该移动设备已经拔掉”?
    2017年3月2日 7:18

答案


  • Hi 轮回的齿轮,

    你可以尝试对USB的插入和拔出进行监控。然后通过获取USB的信息来更新你的集合(默认你实现了INotifyPropertyChanged接口)。这样,你的UI上回对USB的状态实时更新。

    下面的代码你可以参考。

     public partial class CheckUSEStates : Window
        {
    
    
            // usb消息定义
            public const int WM_DEVICE_CHANGE = 0x219;
            public const int DBT_DEVICEARRIVAL = 0x8000;
            public const int DBT_DEVICE_REMOVE_COMPLETE = 0x8004;
            public const UInt32 DBT_DEVTYP_PORT = 0x00000003;
            [StructLayout(LayoutKind.Sequential)]
            struct DEV_BROADCAST_HDR
            {
                public UInt32 dbch_size;
                public UInt32 dbch_devicetype;
                public UInt32 dbch_reserved;
            }
            [StructLayout(LayoutKind.Sequential)]
            protected struct DEV_BROADCAST_PORT_Fixed
            {
                public uint dbcp_size;
                public uint dbcp_devicetype;
                public uint dbcp_reserved;
                // Variable?length field dbcp_name is declared here in the C header file.
            }
    
            public CheckUSEStates()
            {
                InitializeComponent();
              
            }
    
            /// <summary>
            /// 检测USB串口的拔插
            /// </summary>
            /// <param name="m"></param> 
            public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if (msg == WM_DEVICE_CHANGE)        // 捕获USB设备的拔出消息WM_DEVICECHANGE
                {
                    switch (wParam.ToInt32())
                    {
                        case DBT_DEVICE_REMOVE_COMPLETE:    // USB拔出                        
                            break;
                        case DBT_DEVICEARRIVAL:             // USB插入获取对应串口名称
                            DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
                            if (dbhdr.dbch_devicetype == DBT_DEVTYP_PORT)
                            {
                                string portName = Marshal.PtrToStringUni((IntPtr)(lParam.ToInt32() + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT_Fixed))));
                                Console.WriteLine("Port '" + portName + "' arrived.");
                            }
                            break;
                    }
                }
                return hwnd;
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
                if (source != null) source.AddHook(WndProc);
            }
        }

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年3月2日 11:13
    版主