• .Net操作.exe文件


    1 Process proc = new Process(); 
    2 proc.StartInfo.FileName = @"D:Program FilesFoxmailFoxmail.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;命名空间。祝你好运
    亦余心之所善兮,虽九死其犹未悔。夜阑卧听风吹雨,铁马冰河入梦来。临渊羡鱼,不如退而结网。醉卧沙场君莫笑,古来征战几人回!

  • 相关阅读:
    in_array()和explode()的使用笔记
    写sql语句连接的时候注意的一个小细节
    在thinkphp框架模板中引用session
    查询数据库所有(某个)表中字段名,数据类型,说明等,导出数据字典
    (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
    判断当前线程所处的状态 (转)以及终止当前线程
    string 字符串的分隔处理与list的相互转换
    C# 动态调用webservice
    C#中的List<string>泛型类示例
    命名空间"system.web"中不存在类型或命名空间名称security"
  • 原文地址:https://www.cnblogs.com/liujiangping/p/4657095.html
Copyright © 2020-2023  润新知