Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uninstall usb device using c#

Please help me to uninstall usb device in window os. 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.

Like usbdeview software in module "uninstall selected devices". this's link of this soft: http://www.nirsoft.net/utils/usb_devices_view.html

like image 757
fox Avatar asked Nov 19 '12 03:11

fox


2 Answers

Devcon

Got it working by using a commandline tool called devcon, which I then called from code.

Dropped devcon.exe into one of the system paths so it works everywhere.

Devcon: devcon

called: DEVCON Remove usb"MI_01"

then called: DEVCON rescan

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();
like image 101
Talha Avatar answered Oct 15 '22 23:10

Talha


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. But, still can be done using 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.

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.

Anyway your Windows host keeps in mind the device using the PID (Product ID), VID (Vendor ID) and Serial Number of your USB device. 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).

like image 45
garzanti Avatar answered Oct 15 '22 22:10

garzanti