如果笔式驱动器插入 USB 端口,如何使用 c# 检测?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect using c# if a pendrive is plugged into a USB port?
提问by user20358
Is there a way to find out when in a LAN anyone plugs in a pendrive to the USB port? Programatically (in C# preferably) or through some tool. Basically I'd imagine a client application sits on each terminal and monitors the USB ports and sends the information to the server.
有没有办法找出局域网中何时有人将笔式驱动器插入 USB 端口?以编程方式(最好在 C# 中)或通过一些工具。基本上我会想象一个客户端应用程序位于每个终端上并监视 USB 端口并将信息发送到服务器。
a.) Can I get the details of the file(s) being copied? b.) Is there a way to do this without a client application?
a.) 我可以获取正在复制的文件的详细信息吗?b.) 有没有办法在没有客户端应用程序的情况下做到这一点?
EDIT
编辑
I dont want to disable the USB port entirely. its to be on a need to have basis. Basically just want the users on the LAN to share data responsibly and know that whatever data is tranfered is monitored and logged and can be questioned later.
我不想完全禁用 USB 端口。它是基于需要有基础。基本上只是希望局域网上的用户负责任地共享数据,并知道传输的任何数据都受到监控和记录,并且可以在以后提出质疑。
采纳答案by MSalters
[Assuming Windows, given the C# remark. Please tag accordingly]
[假设是 Windows,给出 C# 注释。请相应地标记]
Yes, this is possible. And it is possible to get the details of the file. It will require programming, though. Watch for WM_DEVICECHANGEand re-enumerate drives afterwards. It will get you USB pendrives, but also SD cards. I expect that's a bonus for you.
是的,这是可能的。并且可以获取文件的详细信息。不过,它需要编程。注意WM_DEVICECHANGE并在之后重新枚举驱动器。它将为您提供 USB 闪存盘,以及 SD 卡。我希望这对你来说是一个奖励。
To get more details once you know a drive has arrived, use System.IO.FileSystemWatcher
要在知道驱动器已到达后获取更多详细信息,请使用System.IO.FileSystemWatcher
UpdateI found a better solution - if you register for volume interface notifications, you'll get the volume path for the new drive.
First, create a DEV_BROADCAST_DEVICEINTERFACE
with dbcc_classguid=GUID_DEVINTERFACE_VOLUME
. Then pass this to RegisterDeviceNotification()
. You will again get a WM_DEVICECHANGE but you can now cast the lParam from the message to DEV_BROADCAST_DEVICEINTERFACE*
.
更新我找到了一个更好的解决方案 - 如果您注册卷接口通知,您将获得新驱动器的卷路径。首先,创建一个DEV_BROADCAST_DEVICEINTERFACE
带有dbcc_classguid=GUID_DEVINTERFACE_VOLUME
. 然后将其传递给RegisterDeviceNotification()
. 您将再次获得 WM_DEVICECHANGE,但您现在可以将消息中的 lParam 转换为DEV_BROADCAST_DEVICEINTERFACE*
.
You can pass the dbcc_name
you receive to GetVolumeNameForVolumeMountPoint()
. You can also pass all drive letters from GetLogicalDriveStrings()
to GetVolumeNameForVolumeMountPoint()
. You'll have one matching volume name; this is the new drive.
您可以将dbcc_name
收到的传递给GetVolumeNameForVolumeMountPoint()
。您还可以将所有驱动器号从 传递GetLogicalDriveStrings()
到GetVolumeNameForVolumeMountPoint()
。您将拥有一个匹配的卷名;这是新驱动器。
回答by Nick Berardi
Are you trying to prevent the use of USB thumb drives? If so there is a Group Policy that allows you to restrict access.
您是否试图阻止使用 USB 拇指驱动器?如果是这样,则有一个组策略允许您限制访问。
回答by Jimoc
If its a small environment and you want to prevent any usb devices from being used, then you can disable the usb ports in device manager, make sure no users are set up as adminstrators on the machines and that should prevent all usb use.
如果它是一个小环境并且你想阻止使用任何 USB 设备,那么你可以在设备管理器中禁用 USB 端口,确保没有用户在机器上设置为管理员,这应该阻止所有 USB 使用。
And if you are really paranoid about it, just open the machines and plug out the ports.
如果您真的对此感到偏执,只需打开机器并拔出端口即可。
回答by Stefan
Also check out the registry where all information is stored about the usb devices. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
还要检查存储有关 USB 设备的所有信息的注册表。HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
You can hook into changes of that key in the registry and act upon that.
您可以挂钩注册表中该键的更改并对其进行操作。
This free utility are a big help when pooking around: http://www.nirsoft.net/utils/usb_devices_view.html
这个免费的实用程序是一个很大的帮助:http://www.nirsoft.net/utils/usb_devices_view.html
You can select a usb-drive, choose to open the registry key for that drive and enable/disable the device and a lot more.
您可以选择一个 USB 驱动器,选择打开该驱动器的注册表项并启用/禁用设备等等。
In the registry you can see if the device is connected, if it's of massstorage type and other interresting information. Its easy to filter the regkeys to just get usb-massstorage drives and then hook into and wait for changes (connect/disconnect).
在注册表中,您可以查看设备是否已连接、是否为大容量存储类型以及其他相关信息。很容易过滤注册密钥以获取 USB 大容量存储驱动器,然后挂入并等待更改(连接/断开连接)。
With Windows Management Instrumentation you can register to recieve Registry events: http://msdn.microsoft.com/en-us/library/aa393035(VS.85).aspx
使用 Windows Management Instrumentation,您可以注册以接收注册表事件:http: //msdn.microsoft.com/en-us/library/aa393035(VS.85) .aspx
Check out System.Management in .Net
查看 .Net 中的 System.Management