CMD commands won't work properly under "C#"

238 Views Asked by At

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);
    }
1

There are 1 best solutions below

0
On

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();
    }