C#操作.exe文件

mac2022-06-30  6

 

1 Process proc = new Process(); 2 proc.StartInfo.FileName = @"D:\Program Files\Foxmail\Foxmail.exe"; 3 //可以用绝对路径 4 proc.StartInfo.Arguments = ""; 5 proc.Start();

 注意:使用 Process 类之前 要先添加命名空间

using System.Diagnostics;

 

 

 

辅车相依,唇亡齿寒。纵使晴明无雨色,入云深处亦沾衣。欲渡黄河冰塞川,将登太行雪满山。此曲只应天上有,人间那得几回闻。羁鸟恋旧林,池鱼思故渊。C# 启动外部程序的几种方法: 1. 启动外部程序,不等待其退出。2. 启动外部程序,等待其退出。3. 启动外部程序,无限等待其退出。4. 启动外部程序,通过事件监视其退出。

// using System.Diagnostics;private string appName = "calc.exe";

/// <summary>/// 1. 启动外部程序,不等待其退出/// </summary>private void button1_Click(object sender, EventArgs e){  Process.Start(appName);  MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text,   MessageBoxButtons.OK, MessageBoxIcon.Information);}

/// <summary>/// 2. 启动外部程序,等待其退出/// </summary>private void button2_Click(object sender, EventArgs e){  try  {  Process proc = Process.Start(appName);  if (proc != null)  {  proc.WaitForExit(3000);  if (proc.HasExited)  MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,   MessageBoxButtons.OK, MessageBoxIcon.Information);  else  {  // 如果外部程序没有结束运行则强行终止之。  proc.Kill();  MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text,   MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  }}}  catch (ArgumentException ex)  {  MessageBox.Show(ex.Message, this.Text,   MessageBoxButtons.OK, MessageBoxIcon.Error);  }  }

/// <summary>/// 3. 启动外部程序,无限等待其退出/// </summary>private void button3_Click(object sender, EventArgs e){try{Process proc = Process.Start(appName);if (proc != null){proc.WaitForExit();MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);}}catch (ArgumentException ex){MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);}}

/// <summary>/// 4. 启动外部程序,通过事件监视其退出/// </summary>private void button4_Click(object sender, EventArgs e){try{// 启动外部程序Process proc = Process.Start(appName);if (proc != null){// 监视进程退出proc.EnableRaisingEvents = true;// 指定退出事件方法proc.Exited += new EventHandler(proc_Exited);}}catch (ArgumentException ex){MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);}}/// <summary>/// 外部程序退出事件/// </summary>void proc_Exited(object sender, EventArgs e){MessageBox.Show(String.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.RedirectStandardInput = 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:");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;命名空间。祝你好运 亦余心之所善兮,虽九死其犹未悔。夜阑卧听风吹雨,铁马冰河入梦来。临渊羡鱼,不如退而结网。醉卧沙场君莫笑,古来征战几人回!

转载于:https://www.cnblogs.com/IT1517/p/4768327.html

最新回复(0)