- 1 year ago
Is there an API using C# to change Windows 10 Active Hours?
Comment
1 / 1
Yes, you can change the Active Hours of Windows 10 programmatically using the Windows.Devices.Power namespace in C#. Here's an example of how you can do it:
csharpusing System;
using System.Threading.Tasks;
using Windows.Devices.Power;
using Windows.Foundation;
public class ActiveHoursManager
{
public async Task<bool> SetActiveHours(TimeSpan start, TimeSpan end)
{
if (PowerManager.IsPowerManagerSupported)
{
var powerManager = PowerManager.PowerManagerInstance;
var activeHours = new Range<TimeSpan>(start, end);
var setActiveHoursAsync = powerManager.SetActiveHoursAsync(activeHours);
var result = await setActiveHoursAsync;
return result == PowerSetActiveHoursResult.Success;
}
return false;
}
}
In this example, the SetActiveHours
method takes two TimeSpan
parameters representing the start and end time of the active hours. It returns true
if the active hours are successfully set, otherwise false
.
Note that the SetActiveHours
method is an asynchronous operation, so you may need to use async/await
or handle the Task
returned by the method accordingly.
Make sure to add a reference to the Windows.Devices.Power
namespace in your C# project.
Remember that changing the Active Hours programmatically requires appropriate permissions and access rights.