Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

api - How can I cycle a USB device from C#?

I'd like to cycle (simulate unplug and re-inserting) a USB device (modem) after a certain event has fired. I found a sample on codeproject:

http://www.codeproject.com/KB/system/usbeject.aspx

That allows me to identify+eject the device via its non-volatile serial, but I need it to recycle, not just eject.

I have read this:

http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2005-02/1292.html

I do not understand it.

This has been mentioned in other USB related posts:

http://www.codeproject.com/KB/system/DriveDetector.aspx

It is not relevant to my problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...