天天看点

C#调用外部程序

C#调用外部程序

引用:System.Diagnostics.Process

// 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);

}

/// 2. 启动外部程序,等待其退出

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,

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);

/// 3. 启动外部程序,无限等待其退出

private void button3_Click(object sender, EventArgs e)

proc.WaitForExit();

/// 4. 启动外部程序,通过事件监视其退出

private void button4_Click(object sender, EventArgs e)

// 启动外部程序

// 监视进程退出

proc.EnableRaisingEvents = true;

// 指定退出事件方法

proc.Exited += new EventHandler(proc_Exited);

/// 启动外部程序退出事件

void proc_Exited(object sender, EventArgs e)

总结了一下常用的属性

System.Diagnostics.Process process = System.Diagnostics.Process.Start(@"c:\\test.txt");

process.EnableRaisingEvents = true; //是否激发关闭事业

//process.Exited +=new EventHandler(process_Exited);

// process.ExitTime //获取进程退出时间

// process.HasExited //进程是否已终止

// process.Kill //关闭当前进程

// process.MainWindowTitle //进程窗口标题

// process.ProcessName //该进程的名称

// process.Start //启动进程

// process.StartTime //进程启动时间

// process.WaitForExit //无限期的等待进程退出

process.Close();

process.Dispose();

如果你要运行一个命令行程序,或者打开一个windows应用程序,或者打开默认的web浏览器或email客户端,..你应该如何在你的C#代码中实现这个功能呢?

以下这些例子完成相同的任务,你可以使用System.Diagnostics.Process中的类和方法完成这些任务,甚至作的更多。

例1:不管输出结果,仅仅是运行一个命令行程序:

private void simpleRun_Click(object sender, System.EventArgs e){ System.Diagnostics.Process.Start(@"C:\listfiles.bat");}

例2. 得到程序运行结果等待直到程序中止(同步运行程序)

private void runSyncAndGetResults_Click(object sender, System.EventArgs e){ System.Diagnostics.ProcessStartInfo psi =   new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");

psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; System.Diagnostics.Process listFiles; listFiles = System.Diagnostics.Process.Start(psi); System.IO.StreamReader myOutput = listFiles.StandardOutput; listFiles.WaitForExit(2000);

if (listFiles.HasExited) 

{  string output = myOutput.ReadToEnd();  this.processResults.Text = output; }}

例3. 使用用户机器里的默认浏览器显示URL

private void launchURL_Click(object sender, System.EventArgs e){ string targetURL = @http://www.baidu.com; System.Diagnostics.Process.Start(targetURL);}

我的看法是,同样是打开浏览器显示URL,使用例3种的方法比启动IE并以URL作为参数要来得合理。例3的代码将会启动用户的默认浏览器,而并不总是IE。这样你更有可能给用户带来他们所希望得到的体验,并且可以利用具有最新连接信息的浏览器