简体   繁体   English

如果将Pendrive插入USB端口,如何使用c#进行检测?

[英]How to detect using c# if a pendrive is plugged into a USB port?

Is there a way to find out when in a LAN anyone plugs in a pendrive to the USB port? 有什么方法可以找出在局域网中何时有人将笔式驱动器插入USB端口? Programatically (in C# preferably) or through some tool. 以编程方式(最好使用C#)或通过某些工具。 Basically I'd imagine a client application sits on each terminal and monitors the USB ports and sends the information to the server. 基本上,我会想象一个客户端应用程序位于每个终端上,并监视USB端口并将信息发送到服务器。

a.) Can I get the details of the file(s) being copied? a。)我可以获取要复制的文件的详细信息吗? b.) Is there a way to do this without a client application? b。)有没有客户端应用程序的方法吗?

EDIT 编辑

I dont want to disable the USB port entirely. 我不想完全禁用USB端口。 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. 基本上,只是希望LAN上的用户负责任地共享数据,并且知道要监视和记录传输的任何数据,以后可以提出质疑。

[Assuming Windows, given the C# remark. [假定Windows,给出C#标记。 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. 注意WM_DEVICECHANGE,然后重新枚举驱动器。 It will get you USB pendrives, but also SD cards. 它可以为您提供USB Pendrive和SD卡。 I expect that's a bonus for you. 我希望这对您来说是一个好处。

To get more details once you know a drive has arrived, use System.IO.FileSystemWatcher 若要在知道驱动器到达后获得更多详细信息,请使用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 with dbcc_classguid=GUID_DEVINTERFACE_VOLUME . 首先,创建一个DEV_BROADCAST_DEVICEINTERFACEdbcc_classguid=GUID_DEVINTERFACE_VOLUME Then pass this to RegisterDeviceNotification() . 然后将其传递给RegisterDeviceNotification() You will again get a WM_DEVICECHANGE but you can now cast the lParam from the message to DEV_BROADCAST_DEVICEINTERFACE* . 您将再次获得WM_DEVICECHANGE,但现在可以将消息中的lParam投射到DEV_BROADCAST_DEVICEINTERFACE*

You can pass the dbcc_name you receive to GetVolumeNameForVolumeMountPoint() . 您可以将收到的dbcc_name传递给GetVolumeNameForVolumeMountPoint() You can also pass all drive letters from GetLogicalDriveStrings() to GetVolumeNameForVolumeMountPoint() . 您还可以将所有驱动器号从GetLogicalDriveStrings()传递到GetVolumeNameForVolumeMountPoint() You'll have one matching volume name; 您将拥有一个匹配的卷名; this is the new drive. 这是新驱动器。

Also check out the registry where all information is stored about the usb devices. 还请检查注册表,其中存储了有关USB设备的所有信息。 HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\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/zh-cn/library/aa393035( VS.85) .aspx

Check out System.Management in .Net 在.Net中检出System.Management

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. 而且,如果您对此实在有点偏执,只需打开机器并插入端口即可。

Are you trying to prevent the use of USB thumb drives? 您是否要阻止使用USB拇指驱动器? If so there is a Group Policy that allows you to restrict access. 如果是这样,则存在一个允许您限制访问权限的组策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM