C#监控USB接口
该C#代码实现监控USB接口是否有设备接入或拨出,包括多个U盘。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms; namespace MyApplication
{
public partial class Form1 : Form
{
const int WM_DEVICECHANGE = 0x0219; // 发生设备变动
const int DBT_DEVICEARRIVAL = 0x8000; // 系统检测到一个新设备
const int DBT_DEVTYP_VOLUME = 0x00000002; // 逻辑卷标
const int DBT_CONFIGCHANGECANCELED = 0x0019;
const int DBT_CONFIGCHANGED = 0x0018;
const int DBT_CUSTOMEVENT = 0x8006;
const int DBT_DEVICEQUERYREMOVE = 0x8001;
const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002; // 系统完成移除一个设备
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
const int DBT_DEVICEREMOVEPENDING = 0x8003;
const int DBT_DEVICETYPESPECIFIC = 0x8005;
const int DBT_DEVNODES_CHANGED = 0x0007;
const int DBT_QUERYCHANGECONFIG = 0x0017;
const int DBT_USERDEFINED = 0xFFFF; [StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_VOLUME
{
public int dbcv_size;
public int dbcv_devicetype;
public int dbcv_reserved;
public int dbcv_unitmask;
} public Form1()
{
InitializeComponent();
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE)
{
switch (m.WParam.ToInt32())
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL://U disk upload
DriveInfo[] s = DriveInfo.GetDrives();
foreach (DriveInfo drive in s)
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine("==================================================");
Console.WriteLine("the U disk in! ,name:" + drive.Name.ToString());
break;
}
}
Console.WriteLine("==================================================");
break;
case DBT_CONFIGCHANGECANCELED:
break;
case DBT_CONFIGCHANGED:
break;
case DBT_CUSTOMEVENT:
break;
case DBT_DEVICEQUERYREMOVE:
break;
case DBT_DEVICEQUERYREMOVEFAILED:
break;
case DBT_DEVICEREMOVECOMPLETE: //U disk is out
DriveInfo[] I = DriveInfo.GetDrives();
foreach (DriveInfo DrInfo in I)
{
int devType = Marshal.ReadInt32(m.LParam, );
if (devType == DBT_DEVTYP_VOLUME)
{
DEV_BROADCAST_VOLUME vol;
vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
string ID = vol.dbcv_unitmask.ToString("x");
Console.WriteLine(IO(ID) + ": U disk out!");
}
}
break;
case DBT_DEVICEREMOVEPENDING:
break;
case DBT_DEVICETYPESPECIFIC:
break;
case DBT_DEVNODES_CHANGED:
break;
case DBT_QUERYCHANGECONFIG:
break;
case DBT_USERDEFINED:
break;
default:
break;
}
}
base.WndProc(ref m);
} public string IO(string IO)
{
string Value = "";
switch (IO)
{
case "":
Value = "A:";
break;
case "":
Value = "B:";
break;
case "":
Value = "C:";
break;
case "":
Value = "D:";
break;
case "":
Value = "E:";
break;
case "":
Value = "F:";
break;
case "":
Value = "G:";
break;
case "":
Value = "H:";
break;
case "":
Value = "I:";
break;
case "":
Value = "J:";
break;
case "":
Value = "K:";
break;
case "":
Value = "L:";
break;
case "":
Value = "M:";
break;
case "":
Value = "N:";
break;
case "":
Value = "O:";
break;
case "":
Value = "P:";
break;
case "":
Value = "Q:";
break;
case "":
Value = "R:";
break;
case "":
Value = "S:";
break;
case "":
Value = "T:";
break;
case "":
Value = "U:";
break;
case "":
Value = "V:";
break;
case "":
Value = "W:";
break;
case "":
Value = "X:";
break;
case "":
Value = "Y:";
break;
case "":
Value = "Z:";
break;
default: break;
}
return Value;
}
}
}
C#监控USB接口的更多相关文章
- rockchip 3128 平台USB接口鼠标U盘功能调试日记
2019-04-12 关键词: rk3128 OTG.rk3128外接鼠标U盘.RK外接鼠标U盘 本篇文章是笔者根据自己当前所掌握知识对 rk3128 平台 USB 接口接鼠标实现输入功能调试的一个记 ...
- Python监控网站接口值
Python监控网站接口值: #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'liudong' import urllib,sy ...
- 下一代USB接口将支持双向拔插,于明年亮相
近日,USB接口标准制定组织表示新一代USB接口将于明年年中亮相,而其名称目前被暂定为了USB Type-C.该组织表示USB Type-C标准将允许制造商采用更纤薄的接口设计,在实用性大大提高的同时 ...
- USB接口介绍
USB设备系统分为两个部分,USB Host端和USB Device端,以USB接口的U盘为例子,U盘自身是一个USB Device,PC机的USB接口以及相关的控制电路为USB Host部分 ...
- (九)打印机驱动设置—USB接口的设置
佳博打印机代理商淘宝店https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.Sqz8Pf 在此店购买的打印机 ...
- USB接口定义
一般的排列方式是:红白绿黑从左到右 定义: 红色-USB电源 标有-VCC.Power.5V.5VSB字样 绿色-USB数据线(正)-DATA+.USBD+.PD+.USBDT+ 白色-USB数据线( ...
- 如何禁用电脑USB接口
方法一,BIOS设置法 重新启动计算机,在开机过程中,点击键盘上的“Delete”键,进入BIOS设置界面,选择“Integrated Peripherals”选项,展开后将“USB 1.1 Cont ...
- 突发小事件,USB接口问题
昨天遇到的突发事件,突然USB接口全部瘫了,键盘鼠标全部不能用,换到别人电脑上可以,吓尿了,以为本子主板挂了,但是发现插U盘竟然可以识别而且可以打开,感觉可能是静电问题,果然,彻底关机,拔掉电池,然后 ...
- shell判断USB接口是否有设备插入
#/bin/sh usb_num=$(cat /proc/scsi/scsi | grep "Vendor" | wc -l)if [ $usb_num = 2 ];then ...
随机推荐
- BSS Audio® Introduces Full-Bandwidth Acoustic Echo Cancellation Algorithm for Soundweb London Conferencing Processors
BSS Audio® Introduces Full-Bandwidth Acoustic Echo Cancellation Algorithm for Soundweb London Confer ...
- Android获取TextView显示的字符串宽度
工作上有业务需要判断textview是否换行,我的做法是判断textview要显示的字符串的宽度是否超过我设定的宽度,若超过则会执行换行. 项目中的其他地方也有这样的需求,故直接使用了那一块的代码.如 ...
- 学习之路三十九:新手学习 - Windows API
来到了新公司,一开始就要做个程序去获取另外一个程序里的数据,哇,挑战性很大. 经过两周的学习,终于搞定,主要还是对Windows API有了更多的了解. 文中所有的消息常量,API,结构体都整理出来了 ...
- UDP"打洞"原理
1. NAT分类 根据Stun协议(RFC3489),NAT大致分为下面四类 1) Full Cone 这种NAT内部的机器A连接过外网机器C后,NAT会打开一个端口.然后外网的任何发到这个打开的端口 ...
- VirtualBox中安装CentOS-6.6虚拟机
1. 下载 可以到官网下载,http://mirror.centos.org/centos/ 如果下载速度太慢的话,也可以到163镜像下载: http://mirrors.163.com/centos ...
- CREATE A ENERGY / HEALTH BAR HUD
Now then, let's get started. 1. Open the Play scene which you had created in the previous post. If y ...
- Visual Studio与Eclipse与IntelliJ快捷键对比
- zk框架window之间传值操作
.zul中向Action传递参数: <listcell> <button label="修改" onClick="@command('edit',id= ...
- 【weka应用技术与实践】过滤器
weka中的过滤器主要用于数据预处理阶段对数据集的各种操作. 今天简单地使用一下过滤器: 首先打开一个自带数据集weather.numeric.arff,这是一个关于通过天气条件,气温以及风力等因素来 ...
- MSSQL 之事务订单存储过程
1. 赋值 set 或者 select 运算符 2.全局,局部变量区别,生命域 (全局变量用户不能定义) 3.@@identity 返回最后插入行的标识列的列值. 4.delete 只删除了数 ...