如何使用 C# 检测随身碟是否插入 USB 端口?
有没有办法找出 LAN 中何时有人将随身碟插入 USB 端口? 以编程方式(最好使用 C#)或通过某种工具。 基本上,我想象客户端应用程序位于每个终端上并监视 USB 端口并将信息发送到服务器。
a.) 我可以获得正在复制的文件的详细信息吗? b.) 有没有办法在没有客户端应用程序的情况下做到这一点?
编辑
我不想完全禁用 USB 端口。 这是有必要有基础的。 基本上只是希望 LAN 上的用户负责任地共享数据,并知道传输的任何数据都会受到监视和记录,并且可以在以后提出质疑。
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.
a.) Can I get the details of the file(s) being copied?
b.) Is there a way to do this without a client application?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
[假设 Windows,给出 C# 注释。 请相应标记]
是的,这是可能的。 并且可以获得文件的详细信息。 不过,这需要编程。 监视 WM_DEVICECHANGE 并随后重新枚举驱动器。 它不仅可以为您提供 USB 闪存盘,还可以为您提供 SD 卡。 我希望这对你来说是一个奖励。
要在知道驱动器到达后获取更多详细信息,请使用 System. IO.FileSystemWatcher
更新
我找到了一个更好的解决方案 - 如果您注册卷接口通知,您将获得新驱动器的卷路径。
首先,使用
dbcc_classguid=GUID_DEVINTERFACE_VOLUME
创建一个DEV_BROADCAST_DEVICEINTERFACE
。 然后将其传递给RegisterDeviceNotification()
。 您将再次收到 WM_DEVICECHANGE,但现在可以将消息中的 lParam 强制转换为DEV_BROADCAST_DEVICEINTERFACE*
。您可以将收到的
dbcc_name
传递给GetVolumeNameForVolumeMountPoint()
。 您还可以将所有驱动器号从GetLogicalDriveStrings()
传递到GetVolumeNameForVolumeMountPoint()
。 您将拥有一个匹配的卷名称; 这是新的驱动器。[Assuming Windows, given the C# remark. Please tag accordingly]
Yes, this is possible. And it is possible to get the details of the file. It will require programming, though. Watch for WM_DEVICECHANGE and re-enumerate drives afterwards. It will get you USB pendrives, but also SD cards. I expect that's a bonus for you.
To get more details once you know a drive has arrived, use System.IO.FileSystemWatcher
Update
I 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
withdbcc_classguid=GUID_DEVINTERFACE_VOLUME
. Then pass this toRegisterDeviceNotification()
. You will again get a WM_DEVICECHANGE but you can now cast the lParam from the message toDEV_BROADCAST_DEVICEINTERFACE*
.You can pass the
dbcc_name
you receive toGetVolumeNameForVolumeMountPoint()
. You can also pass all drive letters fromGetLogicalDriveStrings()
toGetVolumeNameForVolumeMountPoint()
. You'll have one matching volume name; this is the new drive.另请检查存储有关 USB 设备的所有信息的注册表。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
您可以挂钩注册表中该键的更改并对其进行操作。
这个免费实用程序在四处寻找时有很大帮助:http://www.nirsoft.net/utils /usb_devices_view.html
您可以选择一个 USB 驱动器,选择打开该驱动器的注册表项并启用/禁用该设备等等。
在注册表中,您可以查看设备是否已连接、是否属于大容量存储类型以及其他有趣的信息。 可以轻松过滤注册表项以获取 USB 大容量存储驱动器,然后挂接并等待更改(连接/断开连接)。
使用 Windows Management Instrumentation,您可以注册以接收注册表事件:
http://msdn.microsoft.com/en-us /library/aa393035(VS.85).aspx
查看 .Net 中的 System.Management
Also check out the registry where all information is stored about the usb devices.
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
You can select a usb-drive, choose to open the registry key for that drive and enable/disable the device and a lot more.
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).
With Windows Management Instrumentation you can register to recieve Registry events:
http://msdn.microsoft.com/en-us/library/aa393035(VS.85).aspx
Check out System.Management in .Net
如果环境较小并且您想阻止使用任何 USB 设备,那么您可以在设备管理器中禁用 USB 端口,确保计算机上没有用户被设置为管理员,这样就可以阻止所有 USB 的使用。
如果你真的对此很偏执,只需打开机器并拔掉端口即可。
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.
And if you are really paranoid about it, just open the machines and plug out the ports.
您是否试图阻止使用 USB 拇指驱动器? 如果是这样,有一个组策略允许您限制访问。
Are you trying to prevent the use of USB thumb drives? If so there is a Group Policy that allows you to restrict access.