Why is the output of the "powercfg" command different in my program than when run in powershell

410 Views Asked by At

I'm trying to create a program to check if there is a process that will prevent my display from going to sleep. I've been doing this manually by running powercfg /requests and I want to run this command on a timer using .NET

Problem is that the outputs that the command gives me is different than when I run powercfg.exe in powershell.

Code that runs the process:

var cmd = new Process { StartInfo = { FileName = "powercfg" } };

using (cmd)
{
    cmd.StartInfo.Arguments = "/requests";

    cmd.StartInfo.UseShellExecute = false;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.Start();

    string output = cmd.StandardOutput.ReadToEnd();
    cmd.WaitForExit();

    OutputTextBox.Text = output;
}

Program output:

DISPLAY:
None.

SYSTEM:
[PROCESS] Legacy Kernel Caller

[PROCESS] Legacy Kernel Caller

[PROCESS] Legacy Kernel Caller
.
.
.

AWAYMODE:
None.

EXECUTION:
[PROCESS] Legacy Kernel Caller

[PROCESS] Legacy Kernel Caller
.
.
.
.


PERFBOOST:
None.

ACTIVELOCKSCREEN:
[PROCESS] Legacy Kernel Caller
.
.
.

PowerShell output

DISPLAY:
[PROCESS] \Device\HarddiskVolume4\Program Files (x86)\Google\Chrome\Application\chrome.exe
Video Wake Lock

SYSTEM:
[DRIVER] NVIDIA High Definition Audio (HDAUDIO\FUNC_01&VEN_10DE&DEV_0084&SUBSYS_1458371A&REV_1001\5&149e3f03&0&0001)
An audio stream is currently in use.

AWAYMODE:
None.

EXECUTION:
[PROCESS] \Device\HarddiskVolume4\Program Files (x86)\Google\Chrome\Application\chrome.exe
Playing audio

PERFBOOST:
None.

ACTIVELOCKSCREEN:
None.

I'm running the program with <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> and VS as Administrator.

1

There are 1 best solutions below

0
On

I encountered the same issue recently. Just in case someone is interested in knowing the answer to this question like I did, the cause is the bit-ness of the build. When i build the program in x86, the result is different. But if i build in x64, the result is as expected.