.net winform 控制电脑关机,重启,注销,主要模拟cmd执行系统命令
首先写要给基础CMD命令执行方法
public void CmdDeal(string cmd)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.StandardInput.WriteLine(cmd);
}
1.控制电脑关机
CmdDeal("shutdown -s -t 0");
2.控制电脑重启
CmdDeal("shutdown -r -t 0");
3.控制电脑注销
使用 DllImport 调用系统底层方法
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
private void zbtlogoff_Click(object sender, EventArgs e)
{
ExitWindowsEx(0, 0);
}