简体   繁体   English

如何使用c#卸载usb设备

[英]How to uninstall usb device using c#

Please help me to uninstall usb device in window os. 请帮我在window os中卸载usb设备。 When one my usb has been removed from my laptop, my window os remember that have one usb has been added in my laptop, now i want to uninstall it. 当我的笔记本电脑中的一个usb已被移除时,我的窗口记住我的笔记本电脑中已经添加了一个USB,现在我想卸载它。

Like usbdeview software in module "uninstall selected devices". 像模块“卸载所选设备”中的usbdeview软件一样。 this's link of this soft: http://www.nirsoft.net/utils/usb_devices_view.html 这是这个软件的链接: http//www.nirsoft.net/utils/usb_devices_view.html

First if you are searching for an utility to clear a driver I don't think that this is the place to ask, maybe superuser. 首先,如果您正在搜索清除驱动程序的实用程序,我不认为这是要求的地方,也许是超级用户。

If you want uninstall a driver yourself from the code, I don't think that you have a class in .net framework to uninstall a driver. 如果你想从代码中自己卸载一个驱动程序,我不认为你在.net框架中有一个类来卸载驱动程序。 But, still can be done using C#. 但是,仍然可以使用C#完成。 You have an Win32 API function for this: DiUninstallDevice it's a native call and the API it's in C. You will have to write a wrapper and using P/Invoke call it from .net. 你有一个Win32 API函数: DiUninstallDevice它是一个本机调用和它在C中的API。你必须编写一个包装器并使用P / Invoke从.net调用它。

The general logic would like this: when you are notified by the Windows OS that your USB device was disconnected you should scan your driver tree, identify your driver and then remove your driver using the above function call. 一般逻辑是这样的:当Windows操作系统通知您USB设备已断开连接时,您应扫描驱动程序树,识别驱动程序,然后使用上述函数调用删除驱动程序。

Anyway your Windows host keeps in mind the device using the PID (Product ID), VID (Vendor ID) and Serial Number of your USB device. 无论如何,您的Windows主机会记住使用PID设备的PID(产品ID),VID(供应商ID)和序列号的设备。 So, when your device is plugged in again it will automatically identify the USB device using these parameters and search for the driver (of course if there is one already installed in the past). 因此,当您的设备再次插入时,它将使用这些参数自动识别USB设备并搜索驱动程序(当然,如果过去已经安装了该驱动程序)。

Devcon 复康

Got it working by using a commandline tool called devcon, which I then called from code. 通过使用名为devcon的命令行工具来实现它,然后我从代码中调用它。

Dropped devcon.exe into one of the system paths so it works everywhere. 将devcon.exe删除到其中一个系统路径中,以便它可以在任何地方使用。

Devcon: devcon Devcon:devcon

called: DEVCON Remove usb"MI_01" 调用:DEVCON删除usb“MI_01”

then called: DEVCON rescan 然后调用:DEVCON重新扫描

code: 码:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.StartInfo.FileName = "DEVCON";
 proc.StartInfo.Arguments = "Remove *usb"*MI_01";
 proc.StartInfo.RedirectStandardError = true;
 proc.StartInfo.RedirectStandardOutput = true;
 proc.StartInfo.UseShellExecute = false;
 proc.Start();

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

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