- 1 year ago
How to import a Windows Power Plan with C#
To import a Windows Power Plan using C#, you can make use of the PowerCfg.exe command-line utility provided by Windows. Here's an example of how you can import a power plan using C#:
csharpusing System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Path to the power scheme file (.pow or .xml)
string powerPlanFile = @"C:\Path\to\power\scheme.pow";
// Execute the powercfg command to import the power plan
Process.Start("powercfg", $"/import \"{powerPlanFile}\"");
Console.WriteLine("Power plan imported successfully.");
}
}
In this example, we use the Process.Start
method to execute the powercfg
command-line utility with the appropriate arguments to import the power plan specified by the powerPlanFile
variable.
Make sure to replace "C:\Path\to\power\scheme.pow"
with the actual path to your power plan file.
Note that the Process.Start
method will execute the command asynchronously, so you may want to add additional logic to wait for the process to complete if necessary.
Additionally, ensure that your application has sufficient permissions to execute the powercfg
command and modify power plans on the system.