以编程方式连接和断开 USB“无需拔下和重新插入" [英] Connect and disconnect USB programmaticaly "WITHOUT UNPLUG AND REPLUG"

查看:52
本文介绍了以编程方式连接和断开 USB“无需拔下和重新插入"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式连接和断开 USB.也就是说,我已经插入了USB设备.我需要使用 C#、.NET 应用程序传输文件.该应用程序将监视特定文件夹并将文件从该文件夹传输到 USB 驱动器.我需要在文件传输后断开 USB 设备并在需要时连接 USB - 无需拔下和重新插入.

I need to connect and disconnect USB programmatically. That is, I have inserted the USB device. I need to transfer the file using C#, .NET application. The application will watch the particular folder and transfer the file from that folder to a USB drive. I need to disconnect the USB device after the file is transfered and connect the USB when needed - without unplug and replug.

有什么代码可以做到这一点,或者是否有任何可用的 DLL 文件?

What would be some code to do it or is there any DLL file available?

主要内容:注意,注意:无需拔下和重新插入 USB 设备.

Main thing: NOTE, NOTE: Without unplugging and replugging the USB device.

推荐答案

如果您的目标是使某个磁盘卷在您不使用时不可用,更明智的方法可能是使用卷管理 API,例如通过使用 IOCTL_VOLUME_OFFLINE 控制代码.(我假设您知道哪个驱动器盘符属于您的 USB 磁盘.)

If your goal is to make a certain disk volume unavailable while you're not using it, a more sensible approach might be to use the volume management APIs, e.g. by using the IOCTL_VOLUME_OFFLINE control code. (I'm assuming that you know which drive letter belongs to your USB disk.)

或者,您可以使用 CfgMgr/SetupAPI 以编程方式禁用和启用卷设备——与在 设备管理器 并选择禁用就可以了.(有关使用 SetupAPI 的信息,请查看 DevCon 示例代码随 Windows WDK 一起提供,有关 SetupDiChangeState.)

Alternatively, you can disable and enable the volume device programmatically with the CfgMgr / SetupAPI -- the same as right-clicking the volume in Device Manager and choosing Disable would do. (For information about using SetupAPI, please review the DevCon sample code provided with the Windows WDK, and see MSDN for functions such as SetupDiChangeState.)

后一个选项可能需要特权用户帐户.

The latter option might require a privileged user account.

这篇关于以编程方式连接和断开 USB“无需拔下和重新插入"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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