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);
}
} }
}
C# usb设备的自动退出的更多相关文章
- Linux自动共享USB设备:udev+Samba
一.概述 公司最近要我实现USB设备插入Ubuntu后,自动共享到网络上,能像Windows共享一样(如\\192.168.1.10)访问里面的内容,不需要写入权限.当时听完这需求,我这新人表示惊呆了 ...
- Linux Shell 脚本学习第一天: 使用grep 命令,lsusb, ps -ef, 实现树莓派(Debian OS)时检测到依赖的USB设备启动后,启动终端自动执行shell脚本
1.应用背景: 无人监测的设备,常需要设置应用程序开机启动,程序启动前需要保证调用的设备先启动,运行环境先启动. 2.test.sh部分源码 #!/bin/sh #查看桌面是否启动 while tru ...
- AD域控制器通过组策略禁止USB设备
问题:域环境下如何禁用USB口设备? 第一种:用传统的办法,在Bios中禁用USB. 第二种: 微软技术支持回答:根据您的需求, Windows识别USB设备主要通过两个文件,一个是Usbstor.p ...
- VMware虚拟机找不到USB设备
VMware虚拟机找不到USB设备该怎么办?打开虚拟机发现竟然找不到usb设备,键盘和鼠标都是usb的,这该怎么办呢?出现这个问题是因为VMUSBArbService服务没有开启,下面分享开启的方法 ...
- VMware虚拟机找不到USB设备该怎么办?
VMware虚拟机找不到USB设备该怎么办?打开虚拟机发现竟然找不到usb设备,键盘和鼠标都是usb的,这该怎么办呢?出现这个问题是因为VMUSBArbService服务没有开启,下面分享开启的方法 ...
- 你的USB设备还安全吗?USB的安全性已从根本上被打破!
前言: USB设备使用方便,但也可能被用来携带恶意软件.病毒,感染计算机系统.通过禁用自动播放功能.杀毒软件查杀.不定期的对设备进行格式化等操作可以确保它是干净的.但它存在的安全问题要比我们想象的更深 ...
- Linux下如何挂载FAT32格式USB设备
挂u盘之前,运行命令cat /proc/partitions,看看现在系统中有哪些分区.插上u盘以后,再次运行上述命令,看看多出来什么分区.通常是sda1. 1.插入U盘 2.输入 fdisk -l ...
- Linux usb子系统(二):USB设备驱动usb-skeleton.c
usb驱动分为通过usbfs操作设备的用户空间驱动,内核空间的内核驱动.两者不能同时进行,否则容易引发对共享资源访问的问题,死锁!使用了内核驱动,就不能在usbfs里驱动该设备. 下面转载的一篇分析u ...
- USB设备---URB请求块
1.urb 结构体 USB 请求块(USB request block,urb)是USB 设备驱动中用来描述与USB 设备通信所用的基本载体和核心数据结构,非常类似于网络设备驱动中的sk_buff 结 ...
随机推荐
- Dreamweaver 中文乱码
定义当前页面的编码属性 Ctrl+j 标题/编码 将编码改成UTF8即可 PhpStorm FILE->Setting->File Encoding->将U ...
- LUA使用虚函数与使用回调函数
------------------虚函数overload-------------------------- --回调:寻路中格子坐标改变 CHero.OnSearchToCellsChange = ...
- java多线程-慎重使用volatile关键字
Java语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量.这两种机制的提出都是为了实现代码线程的安全性.其中 Volatile 变量的同步性较差(但有时它更简单并且开销更低),而 ...
- Spring DI的配置使用
1.1.1 依赖和依赖注入Spring IOC容器的依赖有两层含义:Bean依赖容器和容器注入Bean的依赖资源:a.Bean依赖容器:也就是说Bean要依赖于容器,这里的依赖是指容器负责创建Bean ...
- 每个内存大小:sudo dmidecode -t memory |grep -A16 "Memory Device$" |grep "Size:"
CPU: 型号:grep "model name" /proc/cpuinfo |awk -F ':' '{print $NF}' 数量:lscpu |grep "CPU ...
- 563. Binary Tree Tilt 子节点差的绝对值之和
[抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...
- 136. Single Number唯一的数字
[抄题]: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)
1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...
- easyui 获取当前页签选中的名称
parent.parent.$("#tabs").tabs('getSelected').panel('options').title == "收藏夹管理"
- idea hibernate jpa 生成实体类
0,添加mysql数据库连接 1,生成个hibernate.cfg.xml 2,打开Persisitence 3,Import Databases Schema 4,选择表生成实体类