# 快速关机 **Repository Path**: wzb_td/test ## Basic Information - **Project Name**: 快速关机 - **Description**: 操作CMD实现快速关机,启动即隐藏至系统托盘图标。 - **Primary Language**: C# - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 2 - **Created**: 2022-10-30 - **Last Updated**: 2024-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: Winforms ## README # 快速关机 #### 介绍 操作CMD实现快速关机,启动即隐藏至系统托盘图标。 #### 图片 ![输入图片说明](https://foruda.gitee.com/images/1667334502182620815/1fd18c40_8202315.png "屏幕截图") ![输入图片说明](https://foruda.gitee.com/images/1667334529995755916/2392d021_8202315.png "屏幕截图") #### 核心代码 ``` public void CMDOperator(string cmd) { #region 操作cmd //1.利用Process打开一个cmd的进程 Process process_cmd = new Process();//创建进程对象 process_cmd.StartInfo.FileName = "cmd.exe";//进程打开文件名为“cmd” //2.配置输入输出许可 process_cmd.StartInfo.RedirectStandardError = true;// 重定向错误流 process_cmd.StartInfo.RedirectStandardInput = true;// 重定向输入流 process_cmd.StartInfo.RedirectStandardOutput = true;// 重定向输出流 //3.设置不创建窗体,不使用系统shell process_cmd.StartInfo.CreateNoWindow = true;// TRUE隐藏窗口运行;false在winform下会弹出 process_cmd.StartInfo.UseShellExecute = false;//是否使用系统shell执行,否 //4.进程开启 process_cmd.Start(); //5.CMD执行命令 process_cmd.StandardInput.WriteLine(cmd + "&exit");//加上"&exit"防止进程死循 process_cmd.WaitForExit();//等待程序执行完退出进程 process_cmd.Close();//关闭进程 #endregion 操作cmd } ```