首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试通过C# Process.Start()运行powercfg Process.Start()时出错

尝试通过C# Process.Start()运行powercfg Process.Start()时出错
EN

Stack Overflow用户
提问于 2022-03-24 20:11:07
回答 1查看 238关注 0票数 1

我试图通过一个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 /srumutil
  • powershell.exe Start-Process powercfg /srumutil
  • putting
  • Visual是以管理员身份运行的,进程也是用Verb = "runas"启动的,因此它也应该被提升。
  • ,我已经尝试了C#中的每一个变体,包括(所有这些都失败了):在bat文件中运行它,用Process.Start().

运行bat文件。

其他参数将与像/srumutil一样的Process.Start和Process.Start一起工作,但从不使用powercfg /L

有关守则:

代码语言:javascript
复制
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()函数外,在任何地方都能正常工作。这里有什么东西我遗漏了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-25 07:14:34

我认为您的代码很好,正如您所怀疑的那样,问题就在执行上下文中。当我尝试在非提升的powercfg /srumutil会话中运行PowerShell时,我也会遇到同样的错误,而在提升中,它的工作情况很好,这意味着缺乏管理权限。但是,在这种情况下,错误信息可能具有误导性。在x86进程中运行代码时,而不是在PowerShell和C#中运行x64时,我也会得到相同的错误。尝试检查您的应用程序是x86还是x64,并将其与测试powercfg /srumutil运行良好的PowerShell进程进行比较。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71608828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档