首页 > 解决方案 > 在 WSL 中通过 C++ 关闭和重新启动 PC

问题描述

这是我用 c++ 关闭 PC 的程序,我使用 vs 代码编辑器和 WSL 来运行这个程序:

          #include<iostream>
          #include<stdlib.h>
          int main()
          {

             system("C:\\Windows\\System32\\shutdown /i ");

          }

我收到了这条消息sh: 1: C:WindowsSystem32shutdown: not found

标签: c++systemstdsystem-shutdownsystem-restore

解决方案


确保使用适当的路径。通过 Linux 进行 WSL 的正确形式"/mnt/c/Windows/System32/shutdown.exe"如 prog-fh 和 code_fodder 所述。

所以这会起作用:(我没有在 WSL 中测试过,但上面的用户做了并且知道得更好)

std::system("/mnt/c/Windows/System32/shutdown.exe /i");

或者对于关机,您也可以使用s

std::system("/mnt/c/Windows/System32/shutdown.exe /s"); 

同样,要重新启动,请使用r

std::system("/mnt/c/Windows/System32/shutdown.exe /r");

推荐阅读