/// <summary> /// CMD执行Command命令 /// </summary> /// <returns>执行结果</returns> public MResultObject ExecuteCommandReturnValue(string commondtxt) { MResultObject resultObject = new MResultObject(); try { //this.Writecmd(string.Format("命令{0}", command)); using (Process process = new Process()) { process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = string.Format("/c {0}", commondtxt); process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); // this.Writecmd(string.Format("命令{0}开始执行", command)); process.WaitForExit(20 * 60 * 1000); resultObject.Result = process.ExitCode == 0; resultObject.Message = process.StandardError.ReadToEnd(); // this.Writecmd(string.Format("命令{0}执行完成,执行结果{1}", command, resultObject.Message)); } } catch (Exception ex) { resultObject.Result = false; resultObject.Message = string.Format("执行CMD:“{1}”命令出现异常:{0}", ex.Message, commondtxt); resultObject.ReturnObject = ex; } return resultObject; }
/// <summary> /// CMD执行Command命令 /// </summary> /// <returns>执行结果</returns> public MResultObject ExecuteCommandReturnValue(string commondtxt) { MResultObject resultObject = new MResultObject(); try { //this.Writecmd(string.Format("命令{0}", command)); using (Process process = new Process()) { process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = string.Format("/c {0}", commondtxt); process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); // this.Writecmd(string.Format("命令{0}开始执行", command)); process.WaitForExit(20 * 60 * 1000); resultObject.Result = process.ExitCode == 0; resultObject.Message = process.StandardError.ReadToEnd(); // this.Writecmd(string.Format("命令{0}执行完成,执行结果{1}", command, resultObject.Message)); } } catch (Exception ex) { resultObject.Result = false; resultObject.Message = string.Format("执行CMD:“{1}”命令出现异常:{0}", ex.Message, commondtxt); resultObject.ReturnObject = ex; } return(resultObject); }
/// <summary> /// 执行 /// </summary> public void Exetue() { ControlComputer controlComputer = new ControlComputer(); this.ResultObject = new MResultObject(); try { switch (this.requsetParam.ControlType) { case ControlType.NONE: break; case ControlType.关机: controlComputer.ShutDown(); this.ResultObject.Result = true; break; case ControlType.重启: controlComputer.Restart(); this.ResultObject.Result = true; break; case ControlType.注销: controlComputer.LogOff(); this.ResultObject.Result = true; break; case ControlType.锁定: controlComputer.LockPC(); this.ResultObject.Result = true; break; case ControlType.显示器: controlComputer.Turnoffmonitor(); this.ResultObject.Result = true; break; case ControlType.音量: if (this.requsetParam.VoiceValue > 0) { controlComputer.SetVolUp(); } else if (this.requsetParam.VoiceValue < 0) { controlComputer.SetVolMute(); } this.ResultObject.Result = true; break; case ControlType.命令: this.ResultObject = new ExecuteCommand().ExecuteCommandReturnValue(this.requsetParam.Commendstr); break; default: break; } } catch (Exception ex) { this.ResultObject.Result = false; this.ResultObject.ReturnObject = ex; } }