Using System; using System. collections. generic; using System. IO; using System. linq; using System. runtime. interopServices; using System. text; using System. threading. tasks; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. interop; using System. windows. media; using System. windows. media. imaging; using System. windows. shapes; namespace Wpf_DataGrid_RowDragAndDrop {////// Interaction logic of WinUpan. xaml ///Public partial class WinUpan: Window {public WinUpan () {InitializeComponent ();} private void Window_Loaded (object sender, RoutedEventArgs e) {/* get the Window handle before processing messages in WPF, create an HwndSource object and add * Message Processing callback function through the HwndSource object. hwndSource class: implements its own window process. After creating a window, use AddHook * And RemoveHook to add and remove the hook and receive all window messages. */HwndSource hwndSource = PresentationSource. FromVisual (this) as HwndSource; // if (hwndSource! = Null) {hwndSource. addHook (new HwndSourceHook (WndProc); // hook} string panfu = ""; public const int WM_DEVICECHANGE = 0x219; // After the USB flash drive is inserted, the underlying operating system will automatically detect and send the "hardware device status change" message to the application. public const int DBT_DEVICEARRIVAL = 0x8000; // indicates that the USB flash disk is available. A device or media piece has been inserted and is now available. Public const int DBT_CONFIGCHANGECANCELED = 0x0019; // the request to change the current configuration (or cancel the Dock) has been canceled. Public const int DBT_CONFIGCHANGED = 0x0018; // the current configuration has changed due to the terminal or unfixed. Public const int DBT_CUSTOMEVENT = 0x8006; // custom event occurs. Windows NT 4.0 and Windows 95: this value is not supported. Public const int DBT_DEVICEQUERYREMOVE = 0x8001; // You must delete a device or media file. No application can deny this requirement and cancel the deletion. Public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; // The request to delete a device or media set has been canceled. Public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // a device or media piece has been deleted. Public const int DBT_DEVICEREMOVEPENDING = 0x8003; // a device or media block is about to be deleted. It cannot be denied. Public const int DBT_DEVICETYPESPECIFIC = 0x8005; // a specific device event occurs. Public const int DBT_DEVNODES_CHANGED = 0x0007; // a device has been added or deleted from the system. Public const int DBT_QUERYCHANGECONFIG = 0x0017; // license is required to change the current configuration (pier or unfix ). Public const int DBT_USERDEFINED = 0 xFFFF; // The message indicates that the user-defined public const uint GENERIC_READ = 0x80000000; public const int GENERIC_WRITE = 0x40000000; public const int file_0000_read = 0x1; public const int file_0000_write = 0x2; public const int Limit = 0x2d4808; private IntPtr WndProc (IntPtr hwnd, int msg, IntPtr wParam, intPtr lParam, ref bool handled) {if (msg = WM_DEVICEC HANGE) {switch (wParam. toInt32 () {case DBT_DEVICEARRIVAL: DriveInfo [] s = DriveInfo. getDrives (); s. any (t => {if (t. driveType = DriveType. removable) {panfu = t. name; MessageBox. show ("USB flash drive inserted, drive letter:" + t. name); DirSearch (t. rootDirectory. fullName); return true;} return false;}); break; case DBT_DEVICEREMOVECOMPLETE: MessageBox. show ("unmount USB flash drive"); break; default: break;} return IntPtr. zero;} private Void DirSearch (string path) {try {foreach (string f in Directory. getFiles (path) {listview. items. add (f);} foreach (string d in Directory. getDirectories (path) {DirSearch (d) ;}} catch (Exception) {throw ;}} private void Button_Click (object sender, RoutedEventArgs e) {// The first parameter filename is different from the common file name. The format of the "file name" (often called "device path") of the device driver is fixed as "\. \ DeviceName ", such as string filename = @"\\. \ I: "; string filename = @"\\. \ "+ Panfu. remove (2); // open the device and obtain the device handle. intPtr handle = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, file_pai_read | file_assist_write, IntPtr. zero, 0x3, 0, IntPtr. zero); // send the device control code to the target device, that is, the send command. IOCTL_STORAGE_EJECT_MEDIA the USB flash disk is displayed. Uint byteReturned; bool result = DeviceIoControl (handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr. Zero, 0, IntPtr. Zero, 0, out byteReturned, IntPtr. Zero); MessageBox. Show (result? "U disk exited": "U disk exited failed");} [DllImport ("kernel32.dll", SetLastError = true, CharSet = CharSet. auto)] private static extern IntPtr CreateFile (string lpFileName, uint dwDesireAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint partition, IntPtr hTemplateFile); [DllImport ", exactSpelling = true, SetLastError = true, CharSet = CharSet. auto)] private static extern bool DeviceIoControl (IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint blocks, IntPtr blocks );}}