码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作.exe文件

时间:2015-07-18 16:58:35      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

辅车相依,唇亡齿寒。纵使晴明无雨色,入云深处亦沾衣。欲渡黄河冰塞川,将登太行雪满山。此曲只应天上有,人间那得几回闻。羁鸟恋旧林,池鱼思故渊。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;命名空间。祝你好运
亦余心之所善兮,虽九死其犹未悔。夜阑卧听风吹雨,铁马冰河入梦来。临渊羡鱼,不如退而结网。醉卧沙场君莫笑,古来征战几人回!

C#操作.exe文件

标签:

原文地址:http://www.cnblogs.com/liujiangping/p/4657095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!