Question Help with duplicate code

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.

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();
            }
        }
 
Thanks for everyones replies - I have refactored using the above code and this does what I need for now.
I appreciate it may not be elegant or use the correct calls.

Thanks Folks
 
I see. So you might as well set the values using P/Invoke for the other settings instead of just for the execution thread. The powercfg.exe just calls the same APIs.

In that case set the values in the Windows image ahead of time instead of doing it at the beginning of your testing. Why even set it after the image has been put on the machine?

Anyway, it's your code. Invest as much or as little effort you want into it.
 
This : Question - Help with duplicate code is not what the docs recommend.
Anyway, it's your code. Invest as much or as little effort you want into it.
I guess some people prefer substandard. Cya in a few months when you wan to do it the right way. . .
giphy.gif
 
I see. So you might as well set the values using P/Invoke for the other settings instead of just for the execution thread. The powercfg.exe just calls the same APIs.

In that case set the values in the Windows image ahead of time instead of doing it at the beginning of your testing. Why even set it after the image has been put on the machine?

Anyway, it's your code. Invest as much or as little effort you want into it.

I have no control over the image. I turn up at a customers and use my utility - they set their machines up however they want. I have to work with what is in front of me. I haven't come across the P/Invoke thing, so I will research that and see if I understand how it works. Thanks
 
they set their machines up however they want
... and then you go override their settings. I see. I hope when you leave your customer you restore their original settings.
 
I haven't come across the P/Invoke thing, so I will research that and see if I understand how it works.
There is nothing to research. If you spent some time doing some actual reading on the websites I linked, specifically the pinvoke.net you would find all the info you need at the bottom left of the loading page. They even have a extension for VS.
 
Back
Top Bottom