C# usb设备的自动退出

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using UsbEject;

namespace USBDevice
{
    public class RemoveUsbDevice
    {
        public static string strCurUsb = "";
        public bool bMonitorFlag = false;
        public const int WM_DEVICECHANGE = 0x219;//U盘插入后,OS的底层会自动检测到,然后向应用程序发送“硬件设备状态改变“的消息
        public const int DBT_DEVICEARRIVAL = 0x8000;  //就是用来表示U盘可用的。一个设备或媒体已被插入一块,现在可用。
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;  //要求更改当前的配置(或取消停靠码头)已被取消。
        public const int DBT_CONFIGCHANGED = 0x0018;  //当前的配置发生了变化,由于码头或取消固定。
        public const int DBT_CUSTOMEVENT = 0x8006; //自定义的事件发生。 的Windows NT 4.0和Windows 95:此值不支持。
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;  //审批要求删除一个设备或媒体作品。任何应用程序也不能否认这一要求,并取消删除。
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;  //请求删除一个设备或媒体片已被取消。
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;  //一个设备或媒体片已被删除。
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;  //一个设备或媒体一块即将被删除。不能否认的。
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;  //一个设备特定事件发生。
        public const int DBT_DEVNODES_CHANGED = 0x0007;  //一种设备已被添加到或从系统中删除。
        public const int DBT_QUERYCHANGECONFIG = 0x0017;  //许可是要求改变目前的配置(码头或取消固定)。
        public const int DBT_USERDEFINED = 0xFFFF;  //此消息的含义是用户定义的
        public const uint GENERIC_READ = 0x80000000;
        public const int GENERIC_WRITE = 0x40000000;
        public const int FILE_SHARE_READ = 0x1;
        public const int FILE_SHARE_WRITE = 0x2;
        public const int IOCTL_STORAGE_EJECT_MEDIA = 0x2d4808;

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr CreateFile(
         string lpFileName,
         uint dwDesireAccess,
         uint dwShareMode,
         IntPtr SecurityAttributes,
         uint dwCreationDisposition,
         uint dwFlagsAndAttributes,
         IntPtr hTemplateFile);

        [DllImport("kernel32.dll", 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 lpBytesReturned,
            IntPtr lpOverlapped
        );

        /// <summary>
        /// 卸载当前指定的U盘存储设备
        /// </summary>
        /// <returns>卸载结果</returns>
        public static bool RemoveUsb(string name, string type)
        {
            try
            {
                //第一个参数filename与普通文件名有所不同,设备驱动的“文件名”(常称为“设备路径”)形式固定为“\\.\DeviceName”, 如 string filename = @"\\.\I:";
                //string filename = @"\\.\" + strCurUsb.Remove(2);
                string filename = @"\\.\" + name;
                if (type == "1")
                {
                    filename = @"\\.\" + name.Substring(0, 2);
                }
                //打开设备,得到设备的句柄handle.
                IntPtr handle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, 0x3, 0, IntPtr.Zero);

                // 向目标设备发送设备控制码,也就是发送命令。IOCTL_STORAGE_EJECT_MEDIA  弹出U盘。
                uint byteReturned;
                bool result = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, out byteReturned, IntPtr.Zero);

                return result;
            }
            catch (Exception ex)
            {
                Log.Error("RemoveUsb:" + ex);
                return true;
            }
        }

        public static void RemoveUsbbydll(string name, string type)
        {
            try
            {


                VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
                foreach (Volume device in volumeDeviceClass.Devices)
                {
                    // is this volume on USB disks?
                    if (!device.IsUsb)
                    {
                        continue;
                    }
                    // is this volume a logical disk?
                    if ((device.LogicalDrive == null) || (device.LogicalDrive.Length == 0))
                    {
                        continue;
                    }
                    string filename = name;
                    if (type == "1")
                    {
                        filename = name.Substring(0, 2);
                    }
                    if (device.LogicalDrive == filename)
                    {
                        device.Eject(true); // allow Windows to display any relevant UI
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("RemoveUsbbydll-name:" + ex);
            }
        }

    }
}

  

posted @ 2019-02-01 15:43  Shen_小沈  阅读(990)  评论(1编辑  收藏  举报