Plan
The plan is to disable and subsequently enable a device from inside a windows forms application. To test the first building block of my plan, I open cmd with admin privileges and the following works perfectly:
> devcon hwids =ports
> devcon hwids *VID_10C4*
> devcon disable *VID_10C4*
> devcon enable *VID_10C4*
I can see the device being disabled and enabled again in device manager.
I can also achieve all of this by putting the commands into a batch file and running it from cmd with admin privileges. The above tells me that my plan is essentially good.
Application
However, what I actually want to do is achieve the same thing from inside a windows forms application:
I've set the following in the app manifest:
requestedExecutionLevel level="requireAdministrator" uiAccess="false"
For the sake of baby steps, I have checked this, just to ensure that there are no stupid mistakes in paths and whatnot. And it works just fine. The log file shows me the expected output from the dir command.
// Build String string strCmdText = "'/c cd " + prodPath + " && dir " + " > logs\\logFileEnablePrt.txt \"'"; // Run command var p = new System.Diagnostics.Process(); var psi = new ProcessStartInfo("CMD.exe", strCmdText); psi.Verb = "runas"; // admin rights p.StartInfo = psi; p.Start(); p.WaitForExit();
However, this does not work. It always returns an empty log file and does not change the device as expected:
// Build String string strCmdText = "'/c cd " + prodPath + " && devcon hwids =ports " + " > logs\\logFileEnablePrt.txt \"'"; // Run command var p = new System.Diagnostics.Process(); var psi = new ProcessStartInfo("CMD.exe", strCmdText); psi.Verb = "runas"; // admin rights p.StartInfo = psi; p.Start(); p.WaitForExit();
Error from cmd window is :
'devcon' is not recognized as an internal or external command,
operable program or batch file.
What's going on?
The above has me stumped. I've proved the commands work. I've proved my C# code works. But when I join the 2 together, it doesn't work...
NB: My C# program is running on my D: drive, if that makes any difference...
Updates Based on Comments
@Compo Using your code, it does exactly the same as with mine. I see an empty log file & no changes made to the device. I've altered the /c to /k so I can see what going on the cmd terminal and I see this:
I've even tried your code C:\\Windows\\System32\\devcon hwids =usb
pointing directly at devcon. Also tried \devcon.exe for completeness. The inexplicable error is :
I can see the flipping devcon.exe file sitting right there in the folder! Is there any reason it would not recognise it?
Also, with the command as you wrote it, the log file name is actually named logFileEnablePrt.txt'. I agree that your command looks right, so don't ask me why this happens!
@Panagiotis Kanavos using your code, I get the following error:
This is at the line p.Start();
. I tried putting in devcon.exe, and even the whole path (I checked the folder was in my PATH, and it is). Can't get past this. I actually stumbled on that answer you shared and arrived at this brick wall already.
Here is the code works for me, I don't have ports devices so I change it to usb.