C#中执行Dos命令 C# 本来封装了很多 类似于Dos命令的东西,可是开发需要用它来执行 cmd.texe。
static void Main(string[] args)
{
command.startcmd("ping");
command.cmdPross("ping", "127.0.0.1");
}
public static string cmdPross(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();
Console.WriteLine(output);
cmd.WaitForExit();
cmd.Close();
}
catch (Exception e)
{
Console.WriteLine(e);
}
return output;
}