1、C# 启动外部程序的几种方法:
1. 启动外部程序,不等待其退出。
2. 启动外部程序,等待其退出。
3. 启动外部程序,无限等待其退出。
4. 启动外部程序,通过事件监视其退出。
// using System.Diagnostics;
private string appName = "calc.exe";
///
2、
{
Process.Start(appName);
MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
///
3、 sender, EventArgs e) { try { Process proc = Process.Start(appName); if (proc != null) { proc.WaitForExit(3000); if (proc.HasExited) MessageBox.Show(String.Format("外部程序 {0} 已经退出!", th
4、is.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); else { // 如果外部程序没有结束运行则强行终止之。 proc.Kill(); MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appNa
5、me), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
6、
}
}
///
7、WaitForExit(); MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, this
8、Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
9、 Process.Start(appName); if (proc != null) { // 监视进程退出 proc.EnableRaisingEvents = true; // 指定退出事件方法 proc.Exited += new EventHandler(proc_Exited); } } catch (ArgumentException
10、 ex)
{
MessageBox.Show(ex.Message, this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
11、Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } 用C#调用CMD.exe,执行DOS命令,编码FLV Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.Redi
12、rectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string strOutput=null; // p.StandardInput.WriteLine("cd D:\\flv\\mplayer"); // p.StandardInput.WriteLine("cd d:");
13、 p.StandardInput.WriteLine(string.Format("D:\\flv\\mplayer\\mencoder \"c:\\vs.wmv\" -o \"c:\\output.flv\" -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate={0}:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=4:cmp=6:vb_strategy=1 -vf scale=512:-3 -ofps 12 -srate 22050",200)); p.StandardInput.WriteLine("exit"); strOutput = p.StandardOutput.ReadToEnd(); Console.WriteLine(strOutput); p.WaitForExit(); p.Close(); 记得同时要导入:using System.Diagnostics;命名空间。祝你好运






