'calling devcon from inside windows forms not working

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:

  1. I've set the following in the app manifest:

    requestedExecutionLevel level="requireAdministrator" uiAccess="false"

  2. 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();
    
  3. 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:

enter image description here

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 :

enter image description here

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!

enter image description here

@Panagiotis Kanavos using your code, I get the following error:

enter image description here

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.



Solution 1:[1]

Here is the code works for me, I don't have ports devices so I change it to usb.

public static void Main()
{
    string prodPath = @"c:\devcon\x64";

    // Build String 
    string strCmdText =
    "/c \"cd /d " + prodPath +
    " && devcon hwids =usb " +
    " > log.txt \"";

    // Run command 
    var p = new Process();
    var psi = new ProcessStartInfo("CMD.exe", strCmdText);
    psi.Verb = "runas"; // admin rights
    p.StartInfo = psi;
    p.Start();
    p.WaitForExit();
}

Solution 2:[2]

Worked through a few steps and think I may have an answer...[1]

Just specifying devcon fails as expected...windows cant find the exe as the folder it is in is not in the %PATH% variable in windows..

IF I specify the full path however it works... enter image description here

It wasnt clear from your original code but if your copy of devcon is sitting in either System32 or Syswow directories you may be hitting an emulation issue as well...see here....

EDIT1:: A way to prove this would be to do Direcory.GetFiles(directory containing devcon) and see if the results line up with what you expect

As for passing arguments through to devcon I'd try something like this as opposed to trying to concatenate one giant cmd line..

enter image description here

A similar example but with netstat:

enter image description here

EDIT 2::Another example but with devcon: enter image description here enter image description here enter image description here

The target platform here for the build was x64

EDIT3:: With my application build set to x86:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2