如何使用C#如果一个U盘插入USB端口检测? [英] How to detect using c# if a pendrive is plugged into a USB port?

查看:797
本文介绍了如何使用C#如果一个U盘插入USB端口检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出在当LAN有人在随身碟到USB端口插头?
编程(在C#preferably),或通过一些工具。基本上我想像一个客户端应用程序位于各端子上,并监视USB端口和发送该信息给服务器。

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.

一)。我能得到的文件(S)的详细信息被复制?
湾)有没有办法做到这一点没有一个客户端应用程序?

a.) Can I get the details of the file(s) being copied? b.) Is there a way to do this without a client application?

修改

我不想完全禁用USB端口。其是在需要有基础。基本上只是想在局域网上的用户负责任地共享数据和知道,无论数据是tranfered监视和记录,以后可以质疑的。

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.

推荐答案

[假设的Windows,考虑到C#的话。请相应标签]

[Assuming Windows, given the C# remark. Please tag accordingly]

是的,这是可能的。并且有可能得到的文件的详细信息。这将需要编程,虽然。留意 WM_DEVICECHANGE 并随后重新枚举驱动器。这将让你的USB pendrives,而且SD卡。我希望这是对你的奖金。

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.

要获得更多的细节,一旦你知道的驱动器已经来临,使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx\">System.IO.FileSystemWatcher

To get more details once you know a drive has arrived, use System.IO.FileSystemWatcher

更新
我发现了一个更好的解决方案 - 如果您注册了音量接口通知,你会得到新的驱动器卷路径。
首先,创建一个 DEV_BROADCAST_DEVICEINTERFACE dbcc_classguid = GUID_DEVINTERFACE_VOLUME 。那么这个传递给 RegisterDeviceNotification()。你将再次获得一个WM_DEVICECHANGE,但现在你可以从邮件投中的lParam到 DEV_BROADCAST_DEVICEINTERFACE *

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. 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*.

您可以通过 dbcc_name 您收到 GetVolumeNameForVolumeMountPoint()。您也可以通过所有驱动器号从 GetLogicalDriveStrings() GetVolumeNameForVolumeMountPoint()。您将有一个匹配的卷名;这是新的驱动器。

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.

这篇关于如何使用C#如果一个U盘插入USB端口检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆