try
{
System.Diagnostics.Process.Start([FileName],[Parameter]);
}
catch(Win32Exception win32ex)
{
MessageBox.Show( "Error: "+win32ex.Message, "Error ",MessageBoxButtons.OK,MessageBoxIcon.Error)
}
----------------------------------------------------------------------------------------------------------
using System;
class test
{
static void Main()
{
//声明一个程序信息类
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//设置外部程序名
Info.FileName = "notepad.exe";
//设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments = "test.txt";
//设置外部程序工作目录为 C:
Info.WorkingDirectory = "C:"";
//声明一个程序类
System.Diagnostics.Process Proc ;
try
{
//启动外部程序
Proc = System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception e)
{
Console.WriteLine("系统找不到指定的程序文件。 {0}", e);
return;
}
//打印出外部程序的开始执行时间
Console.WriteLine("外部程序的开始执行时间:{0}", Proc.StartTime);
//等待3秒钟
Proc.WaitForExit(3000);
//如果这个外部程序没有结束运行则对其强行终止
if(Proc.HasExited == false)
{
Console.WriteLine("由主程序强行终止外部程序的运行!");
Proc.Kill();
}
else
{Console.WriteLine("由外部程序正常退出!");}
Console.WriteLine("外部程序的结束运行时间:{0}", Proc.ExitTime);
Console.WriteLine("外部程序在结束运行时的返回值:{0}", Proc.ExitCode);
}}
使用System.Diagnostics.Process类可以启动和停止程序,这个类的Start函数可用于启动一个exe文件。
比如,如果想在浏览器中打开一个URL,就可以将URL作为参数传递给Start函数:
System.Diagnostics.Process.Start(cnblogs/zhangwei720);
============================================================================================================
引用:System.Diagnostics.Process
// using System.Diagnostics;
private string appName = "calc.exe";
///
/// 1. 启动外部程序,不等待其退出
/// 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,
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);
}
}
///
/// 3. 启动外部程序,无限等待其退出
/// 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,
MessageBoxB