0

Can you look at the code and tell me why it won't set-up my powerconfig? It works fine when launched as batch script (so config files are alright), but not when i run same commands under C#

        public void setPowerProfileLaptop()
    {
        string strCmdText;
        strCmdText = "/C REGEDIT /s C:\\Configs\\Enable_Sleep.reg";
        Process.Start("CMD.exe", strCmdText);
        strCmdText = "/C POWERCFG -Import C:\\Configs\\Chillblast.pow affd6254-c7dd-457c-a259-da407eb5ac00";
        Process.Start("CMD.exe", strCmdText);
        strCmdText = "/C POWERCFG -SetActive affd6254-c7dd-457c-a259-da407eb5ac00";
        Process.Start("CMD.exe", strCmdText);
    }
arti
  • 645
  • 1
  • 8
  • 27
  • 2
    `Process.Start` does not wait for the command to complete before returning. You might want to `WaitForExit` on the process object. And check the `ExitCode` to see if any commands fail. – Blorgbeard Aug 14 '14 at 07:48
  • WaitForProcess works perfectly! Thanks! I'll put fixed code as answer if it will allow me – arti Aug 14 '14 at 08:45

1 Answers1

2

Here is fixed code as suggested by Blorgbeard in comment

        public void setPowerProfileLaptop()
    {
        string strCmdText;
        strCmdText = "/C REGEDIT /s C:\\Configs\\Enable_Sleep.reg";
        var enableSleep = Process.Start("CMD.exe", strCmdText);
        enableSleep.WaitForExit();
        strCmdText = "/C POWERCFG -Import C:\\Configs\\Chillblast.pow affd6254-c7dd-457c-a259-da407eb5ac00";
        var importCFG = Process.Start("CMD.exe", strCmdText);
        importCFG.WaitForExit();
        strCmdText = "/C POWERCFG -SetActive affd6254-c7dd-457c-a259-da407eb5ac00";
        var setActive = Process.Start("CMD.exe", strCmdText);
        setActive.WaitForExit();
    }
arti
  • 645
  • 1
  • 8
  • 27