我试图通过一个powercfg /srumutil /output C:\\EnergyConsumption\\srumutil.csv /csv应用程序运行命令C#。上面的命令在powershell和cmd中成功运行,但它在通过我的C#程序执行时返回以下错误:
Unable to perform operation. An unexpected error (0x1f) has occured: A device attached to the system is not functioning.
我会澄清:
在这两种情况下,我都是在带有电池(膝上型电脑)的机器上运行这个命令。
cmd.exe /K powercfg /srumutilpowershell.exe Start-Process powercfg /srumutilVerb = "runas"启动的,因此它也应该被提升。运行bat文件。
其他参数将与像/srumutil一样的Process.Start和Process.Start一起工作,但从不使用powercfg /L。
有关守则:
using System;
using System.Diagnostics;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var info = new ProcessStartInfo()
{
FileName = "powercfg.exe",
Arguments = "/srumutil /output C:\\EnergyConsumption\\srumutil.csv /csv",
Verb = "runas",
UseShellExecute = false
};
var proc = Process.Start(info);
proc.WaitForExit();
Console.ReadLine();
}
}
}长话短说,powercfg /srumutil除通过Process.Start()函数外,在任何地方都能正常工作。这里有什么东西我遗漏了吗?
发布于 2022-03-25 07:14:34
我认为您的代码很好,正如您所怀疑的那样,问题就在执行上下文中。当我尝试在非提升的powercfg /srumutil会话中运行PowerShell时,我也会遇到同样的错误,而在提升中,它的工作情况很好,这意味着缺乏管理权限。但是,在这种情况下,错误信息可能具有误导性。在x86进程中运行代码时,而不是在PowerShell和C#中运行x64时,我也会得到相同的错误。尝试检查您的应用程序是x86还是x64,并将其与测试powercfg /srumutil运行良好的PowerShell进程进行比较。
https://stackoverflow.com/questions/71608828
复制相似问题