private void ParsePowercfgList(string output) { var matches = output.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(scheme => POWER_SCHEME_REGEX.Match(scheme)) .Where(match => match.Success) .ToList(); if (matches.Count == 0) { throw new InvalidOperationException("Could not parse the output from powercfg."); } Plans = new List <PowerPlan>(); ActivePlan = null; foreach (var match in matches) { var powerPlan = new PowerPlan(match.Groups[1].Value, match.Groups[2].Value); Plans.Add(powerPlan); if (match.Groups[3].Success) { ActivePlan = powerPlan; } } if (ActivePlan == null) { throw new InvalidOperationException("Unable to determine the active power plan"); } }
private void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) { string guid = null; if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionLock) { // Locked Console.WriteLine("Workstation locked"); guid = Properties.Settings.Default.LockPlan; } else if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock) { // Unlocked Console.WriteLine("Workstation unlocked"); guid = Properties.Settings.Default.UnlockPlan; } if (guid != null && powerPlans.ActivePlan.Guid != guid) { PowerPlan plan = powerPlans.Plans.Find(p => p.Guid == guid); if (plan != null) { powerPlans.SetActivePlan(plan); } } }
private void SysTrayIcon_SetActivePlan(object sender, SetActivePlanEventArgs e) { PowerPlan newActive = powerPlans.Plans.Find(plan => plan.Guid == e.Guid); if (newActive != powerPlans.ActivePlan) { powerPlans.SetActivePlan(newActive); } }
public void SetActivePlan(PowerPlan plan) { if (plan == null) { throw new ArgumentNullException("plan"); } if (!Plans.Contains(plan)) { throw new ArgumentException("Given plan is not in the list of plans", "plan"); } Console.WriteLine("Setting active plan to " + plan.Name); PowercfgSetActive(plan.Guid); Reload(); }
public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } else if (obj == this) { return(true); } PowerPlan other = (PowerPlan)obj; return(Guid == other.Guid && Name == other.Name); }