简体   繁体   English

如何使用 C# 导入 Windows 电源计划

[英]How to import a Windows Power Plan with C#

I'm working on a small C# app that will import a power plan to the user's PC and set it as active.我正在开发一个小型 C# 应用程序,该应用程序会将电源计划导入用户的 PC 并将其设置为活动状态。 It working perfectly with a.bat file when the.pow file is in the same folder and I'm running commands:当 .pow 文件位于同一文件夹中并且我正在运行命令时,它与 .bat 文件完美配合:

powercfg -import "%~dp0\Optimized.pow"
powercfg /setactive 62ffd265-db94-4d48-bb7a-183c87641f85

Now, in C# I tried this:现在,在 C# 我试过这个:

  Process cmd = new Process();
  cmd.StartInfo.FileName = "powercfg";
  cmd.StartInfo.Arguments = "-import \"%~dp0\\Optimized\"";
  cmd.StartInfo.Arguments = "powercfg /setactive 62ffd265-db94-4d48-bb7a-183c87641f85";
  cmd.Start(); 

  //and this:
  private void button1_Click(object sender, EventArgs e)
  {
      Process cmd = new Process();
      cmd.StartInfo.FileName = "cmd.exe";
      cmd.StartInfo.RedirectStandardInput = true;
      cmd.StartInfo.RedirectStandardOutput = true;
      cmd.StartInfo.CreateNoWindow = true;
      cmd.StartInfo.UseShellExecute = false;
      cmd.Start();
      cmd.StandardInput.WriteLine("powercfg -import \"%~dp0\\Optimized\"");
      cmd.StandardInput.WriteLine("powercfg /setactive 6aa8c469-317b-45d9-a69c-f24d53e3aff5");
      cmd.StandardInput.Flush();
      cmd.StandardInput.Close();
      cmd.WaitForExit();
      Console.WriteLine(cmd.StandardOutput.ReadToEnd());
  }

But the program doesn't see the.pow file in the project folder (I actually tried to put it in each and every folder in the project).但是程序在项目文件夹中没有看到.pow文件(我实际上尝试将它放在项目中的每个文件夹中)。 How it can be implemented to let the powercfg see the file?如何实现让 powercfg 看到文件?

Any help is much appreciated!任何帮助深表感谢! Thanks!谢谢!

You could try something like this:你可以尝试这样的事情:

var cmd = new Process {StartInfo = {FileName = "powercfg"}};
using (cmd) //This is here because Process implements IDisposable
{

   var inputPath = Path.Combine(Environment.CurrentDirectory, "Optimized.pow");

   //This hides the resulting popup window
   cmd.StartInfo.CreateNoWindow = true;
   cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

   //Prepare a guid for this new import
   var guidString = Guid.NewGuid().ToString("D"); //Guid without braces

   //Import the new power plan
   cmd.StartInfo.Arguments = $"-import \"{inputPath}\" {guidString}";
   cmd.Start();

   //Set the new power plan as active
   cmd.StartInfo.Arguments = $"/setactive {guidString}";
   cmd.Start();
}

This fixes the Arguments parameter that is being overwritten/used twice, as well as correctly disposes of the cmd variable.这修复了被覆盖/使用两次的Arguments参数,以及正确处理cmd变量。 Additional lines added to hide the resulting pop-up window, and for generating the Guid upfront and specifying it as part of the command line.添加了其他行以隐藏生成的弹出窗口 window,并用于预先生成 Guid 并将其指定为命令行的一部分。

Your first snippet does not work because you're reassigning cmd.StartInfo.Arguments before executing the process.您的第一个片段不起作用,因为您在执行该过程之前要重新分配cmd.StartInfo.Arguments The first assignment is lost when you throw it out in favor of the second assignment.当您将第一个作业扔掉以支持第二个作业时,它就会丢失。

The first snippet most likely doesn't work because when you set cmd.startInfo.FileName to just a filename with no path, it will search only the directory of your C# app's.exe (likely in project/bin/Debug/ ).第一个片段很可能不起作用,因为当您将cmd.startInfo.FileName设置为没有路径的文件名时,它只会搜索 C# 应用程序的.exe 的目录(可能在project/bin/Debug/中)。 Since the FileName is cmd.exe and there is probably no cmd.exe in your project folder, it can't find anything.由于文件名是cmd.exe并且您的项目文件夹中可能没有cmd.exe ,因此它找不到任何内容。

You may also consider setting cmd.StartInfo.WorkingDirectory to an appropriate directory with your .pow file so that your relative paths will resolve correctly.您还可以考虑将cmd.StartInfo.WorkingDirectory设置为包含.pow文件的适当目录,以便您的相对路径能够正确解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM