0

I am having a slightly odd problem.. the following lines run fine directly in powershell:

1) powercfg -AVAILABLESLEEPSTATES    
2) powercfg -energy

Straight forward enough, -energy generates its file, has other flags I could play with.

Running line 1 from C# works fine too (in any of the wonderful methods throughout this site, like

Powershell s_ps = PowerShell.Create();
s_ps.AddScript("powercfg -AVAILABLESLEEPSTATES");
Collection<PSObject> results = s_ps.Invoke();

(or the versions that run everything through a Pipeline, or create PSCommand(), and so on)

Running the same thing on the -energy works fine from the console, but if I try to call it through C# it starts talking about missing 'energy.dll' or one of its dependencies. All the dlls (including dependencies) are of course there - since it runs from command line, and verified manually anyway.

Visual Studio is running in Admin mode, and just to be on the safe side I built the ap and tried running that directly in Admin mode too

I have tried manually loading the dll

 s_ps.AddScript(@"[Reflection.Assembly]::LoadFrom('C:\Windows\System32\energy.dll') | Out-Null");

But it just throws an extra error saying it

'could not load file or assembly 'file:///C:\Windows\System32\energy.dll' or one of its dependencies'

Does anyone have any thoughts on what else would be causing issues? (Have to run for a bit, but if I find a solution before someone else I will of course post it, been hammering at it for most of the day though with no luck)

4

1 回答 1

0

至于 -engery 是对命令的切换,它必须正确理解并在不要求组装的情况下执行它。唯一的问题是,由于 powercfg.exe -energy 使用异步模型执行,应用程序需要等待 60 秒才能获得结果。我尝试按以下方式执行并工作。我的环境是 Win 7 pro,i7,64bit

foreach (var v in PowerShellInterop.RunPowerShellScript("powercfg -energy"))
            Console.WriteLine(v);
    internal static IEnumerable RunPowerShellScript(string script)
    {
        PowerShell ps = PowerShell.Create();
        ps.AddScript(script);
        Collection<PSObject> result = ps.Invoke();
        foreach (PSObject obj in result)
        {
            yield return (obj);
        }
    }

结果

启用跟踪 60 秒...观察系统行为...分析跟踪数据...分析完成。

发现能源效率问题。

5 错误 19 警告 40 信息

有关更多详细信息,请参见 {path}\Debug\energy-report.html。

于 2013-05-06T07:46:59.660 回答