C# 执行cmd命令
C#如何执行cmd命令
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; //新添加 using System.Diagnostics; //新添加 using log4net; //日志 3.5 或 4.0 net framework下 namespace rankFlush { class command { private static ILog log; public command() { log4net.Config.XmlConfigurator.Configure(); command.log = LogManager.GetLogger("command"); } public static string startcmd(string command) { string output = ""; try { Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { log.Info(e.ToString()); } return output; } public static string startcmd(string command, string argument) { string output = ""; try { Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.Arguments = argument; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { log.Info(e.ToString()); } return output; } } }

浙公网安备 33010602011771号