断开/连接USB存储设备c#.net [英] Disconnect/connect USB storage device c#.net

查看:257
本文介绍了断开/连接USB存储设备c#.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我们开发了一些带有生物识别USB设备的应用程序,可以在Windows应用程序中登录。它除了设备挂起问题外完美无缺。经过这么多尝试或者可以在15天的时间间隔内说,设备卡住了,只能在重新插入后工作。



我们试图解决但有时候它仍然出现在任何机器上。因此,我们正在寻找一些其他方式通过编程方式断开/连接刷新按钮。



生物识别设备在硬盘上不显示为驱动器并且具有存储空间容量也是。



提前致谢...!



我尝试过的:



尝试了一些代码以及通过注册表设置尝试但运气不好。

Hi Guys,

We have developed some application with Biometric USB device to login in windows application. It works perfectly except device hang issue. After so many attempts or can say with in 15 days interval, device got stuck and only work after Reinsert.

We tried to solve but still it comes sometimes on any machines. So we are looking some other way to disconnect/connect through programatically on refresh button.

Biometric device doesn't show as "Drive" on hard disk and its having storage capacity as well.

Thanks in advance...!

What I have tried:

Tried some piece of code as well as tried via registry settings as well but hard luck.

推荐答案

此解决方案中的方法将使用C API。所以你必须使用PInvoke和C#。



最干净的方法是使用提供重置调用的特定于设备的API库。此类库通常作为SDK的一部分提供。通过搜索网络或联系制造商,检查是否为您的设备提供了此类SDK。如果是,请检查它是否提供重置功能。此类API通常还提供查找当前设备的功能。



对于其他方法,您首先要识别设备或与其连接的USB集线器。另请注意,这些需要管理员权限。



对于大多数其他方法,您必须使用Windows SetupAPI。文章 C#硬件助手库 [ ^ ]和来源可能会有所帮助。



第一项任务是使用SetupAPI枚举函数之一识别设备。如果要访问设备本身,可以从INF安装文件中获取设备GUID。要枚举集线器,请使用 GUID_DEVINTERFACE_USB_HUB



枚举USB集线器后,您可以为每个子集枚举(可能也是集线器),直到找到您的设备。然后,您可以使用 CM_Disable_DevNode函数| Microsoft Docs [ ^ ]和 CM_Enable_DevNode function | Microsoft Docs [ ^ ]尝试让设备再次进入正常状态。请注意,这可能不适用于所有设备和Windows版本。



最后你可以进入低级并使用 LibUsbDotNet C#USB库下载| SourceForge.net [ ^ ]。这样您就可以切换(禁用并再次启用)USB集线器和端口,从而导致连接的设备被移除并重新插入。
The methods from this solution will use C APIs. So you would have to use PInvoke with C#.

The cleanest method would be using a device specific API library that provides a reset call. Such libraries are usually provided as part of an SDK. Check if such an SDK is provided for your device by searching the web or contacting the manufacturer. If so, check if it provides a reset function. Such APIs provide usually also a function to find present devices.

For the other methods you have first to identify the device or the USB hub to which it is connected. Note also that these require administrator priviliges.

For most of the other methods you have to use the Windows SetupAPI. The article Hardware Helper Library for C#[^] and the sources may be helpful.

The first task is to identify the device(s) using one of the SetupAPI enumeration functions. If you want to acces the device itself you can get the device GUID from the INF installation file. To enumerate the hubs use the GUID_DEVINTERFACE_USB_HUB.

Having enumerated the USB hubs, you can for each enumerate the childs (which may be also hubs) until you find your device. Then you can use the CM_Disable_DevNode function | Microsoft Docs[^] and CM_Enable_DevNode function | Microsoft Docs[^] to try getting the device into a proper state again. Note that this might not work with all devices and Windows versions.

Finally you can go low level and use the LibUsbDotNet C# USB Library download | SourceForge.net[^]. This allwos you to toggle (disable and enable again) the USB hubs and ports which results in the connected devices to be removed and inserted again.


这篇关于断开/连接USB存储设备c#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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