简体   繁体   中英

How to disconnect a connected device via USB using C#?

I am using C# and I would like to disconnect a connected device that is connected on COM4. I have tried to use SerialPort but it gives access denied and its not reliable even if I add delay between open and close port.

You could download devcon.exe as explained here .
If you are brave enough, you could trust DevCon Installer .

Then, run something like the following from within your code:

    Process p= new Process();

    p.StartInfo.FileName   = "devcon.exe";
    p.StartInfo.Arguments = "disable *SanDisk*";

    p.Start();

My example disconnects a SanDisk USB flashdrive. But this should be able to disconnect any USB device.

How to find the devices for specific ports is explained here :

devcon.exe FindAll =Ports

Some sub-commands of devcon.exe do require administrative privileges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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