colins5286
Member
- Joined
- Nov 8, 2018
- Messages
- 12
- Programming Experience
- Beginner
I have written some code with alters the power settings on Windows computers. I am no software dev, but I suspect there is a better way of coding than what I have written. Basically the code is duplicated 6 times to run each alteration to the system. I am sure there must be a way to write one block of code and just pass the amended parameters to it 6 times...
I have copied the first two sections, but it repeats a further 4 times, just with different arguments.
I have copied the first two sections, but it repeats a further 4 times, just with different arguments.
Duplicate Code:
using System.Diagnostics;
namespace VolatileDataCapture.Services
{
class PowerControlService
{
public void ChangePowerSettings()
{
ChangeStandbyTimeAC();
ChangeStandbyTimeDC();
ChangeHibernateTimeAC();
ChangeHibernateTimeDC();
ChangeMonitorTimeAC();
ChangeMonitorTimeDC();
}
private void ChangeStandbyTimeAC()
{
using (var cmdPwrCfg = new Process())
{
cmdPwrCfg.StartInfo.FileName = "powercfg";
cmdPwrCfg.StartInfo.UseShellExecute = false;
cmdPwrCfg.StartInfo.CreateNoWindow = true;
cmdPwrCfg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmdPwrCfg.StartInfo.Arguments = "-change -standby-timeout-ac 0";
cmdPwrCfg.StartInfo.RedirectStandardOutput = true;
cmdPwrCfg.Start();
}
}
private void ChangeStandbyTimeDC()
{
using (var cmdPwrCfg = new Process())
{
cmdPwrCfg.StartInfo.FileName = "powercfg";
cmdPwrCfg.StartInfo.UseShellExecute = false;
cmdPwrCfg.StartInfo.CreateNoWindow = true;
cmdPwrCfg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmdPwrCfg.StartInfo.Arguments = "-change -standby-timeout-dc 0";
cmdPwrCfg.StartInfo.RedirectStandardOutput = true;
cmdPwrCfg.Start();
}
}