本文演示了如何在C#中使用命令行代码:
string command = System.Configuration.ConfigurationSettings.AppSettings["Command"];//ping www.baidu.com try { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c " + command; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); outputMsg = p.StandardOutput.ReadToEnd(); p.Close(); if (outputMsg.Contains("Reply")) { isComputerOn = "SUCCESS"; return true; } else { isComputerOn = "ERROR"; return false; } } catch (Exception ex) { return false; } }