C#编程的时候有很多时候需要启动外部程序(包括命令行程序或者应用程序)。
C# 启动命令行程序
例如:做上网拨号程序可以启动命令行Rasdial 程序。
---------------------------------------------------------------
Process pro = new Process();
pro.StartInfo.FileName = "cmd";
pro.StartInfo.Arguments = "/c" + "Rasdial/?";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.CreateNoWindow = true;
pro.Start();
//string.rdText = pro.StandardOutput.ReadToEnd(); //执行的结果内容 pro.StandardInput.WriteLine("exit"); //要退出,不然执行下一个程序时候会出错
例如:
用C#调用CMD.exe,执行DOS命令,编码 FLV
---------------------------------------------------------------
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string strOutput=null;
// p.StandardInput.