本文汇总了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();
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#使用process.start启动程序报错解决方法

Unknown error (0xffffffff)
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName, String arguments)
at ProcessStart.Form1.start()
[/code}
出错情景:
我们发现大多数情况下,C#调用Process.Start根本不会出错。这个错误通常出现在当你使用Local System帐号运行程序时,例如我们有一个windows服务,此服务调用Process.Start创建新进程时,新进程及其所有的子进程都是以System帐号运行的。这时调用Process.Start就有可能出现此错误,只是有可能,其实在我们那么多机器上只有一台运行windows 2003的服务器出现了这个错误。可能与系统设置有关,深层原因有待考察。
解决方法:
只要修改代码,设置ProcessStartInfo的UseShellExecute=false即可
[code]
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = exepath;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process.Start(psi);

[C#.Net]启动外部程序的几种常用方法汇总的更多相关文章

  1. C# 启动外部程序的几种常用方法汇总

    . 启动外部程序,不等待其退出. . 启动外部程序,等待其退出. . 启动外部程序,无限等待其退出. . 启动外部程序,通过事件监视其退出. 实现代码如下: // using System.Diagn ...

  2. C# 启动外部程序的几种方法

    . 启动外部程序,不等待其退出. . 启动外部程序,等待其退出. . 启动外部程序,无限等待其退出. . 启动外部程序,通过事件监视其退出. // using System.Diagnostics; ...

  3. C#单独启动进程的几种方式

    本文实例讲述了C#启动进程的几种常用方法.分享给大家供大家参考.具体如下: 1.启动子进程,不等待子进程结束 private void simpleRun_Click(object sender, S ...

  4. C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式

    C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...

  5. C#启动外部程序以及等待外部程序关闭的几种方法

    1. 启动外部程序,不等待其退出. 2. 启动外部程序,等待其退出. 3. 启动外部程序,无限等待其退出. 4. 启动外部程序,通过事件监视其退出. // using System.Diagnosti ...

  6. C#程序实现软件开机自动启动的两种常用方法

    C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法函数的示例与实例带详细注释 方法一:将软件的快捷方式创建到计算机的自动启动目录下(不需要管理员权限) 1.必要引用 ...

  7. python脚本调用外部程序的若干种方式以及利弊

    脚本执行外部程序的常用几种方式: # os.popen(path)# subprocess.run(cmd,shell=True)# subprocess.check_call(cmd,shell = ...

  8. Java中从控制台输入数据的几种常用方法

    Java中从控制台输入数据的几种常用方法 一.使用标准输入串System.in //System.in.read()一次只读入一个字节数据,而我们通常要取得一个字符串或一组数字 //System.in ...

  9. jQuery验证元素是否为空的两种常用方法

    这篇文章主要介绍了jQuery验证元素是否为空的两种常用方法,实例分析了两种常用的判断为空技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了jQuery验证元素是否为空的两种常用方法.分享给 ...

随机推荐

  1. oracle 函数的返回值与out参数

    函数的return值是调用函数返回的结果. 而out参数就是单纯的赋值. 例子: function test(aaa in varchar, bbb out integer) return integ ...

  2. 彻底弄懂css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...

  3. gridview空间使用

    1.HTML代码 <asp:GridView ID="gv_Info" runat="server" AutoGenerateColumns=" ...

  4. 前端 websocket用法

    <!DOCTYPE html> <meta charset="utf-8" /> <title>WebSocket Test</title ...

  5. AngularJs中url参数的获取

    前言: angular获取通过链接形式访问的页面,要获取url中的参数,就不能通过路由的方式传递获取了,使用原生js或者jquery,又显得比较麻烦,好在angular已经封装了获取url参数的方法, ...

  6. css控制div上浮下落

    CSS3 示例:http://www.w3school.com.cn/cssref/pr_keyframes.asp 以下是代码: <!DOCTYPE html> <html> ...

  7. 8.17 纯css画一个着重号图标

    今天看到一个同事写的着重号图标,我以为是图片,仔细一看,是span标签!哇!!学习一下哈哈 图标长这样: CSS代码: .hint{ display: inline-block; width: 20p ...

  8. docker-ce-17.09 网络基础配置

    一.端口映射实现访问容器 1.我们先从pull一个nginx镜像,然后后台运行该镜像 > docker pull nginx > docker run -d -P nginx:latest ...

  9. Struts框架的数据封装二之模型驱动方式

    Struts2中提供了两类数据封装的方式? * 第二种方式:模型驱动 > 使用模型驱动的方式,也可以把表单中的数据直接封装到一个JavaBean的对象中,并且表单的写法和之前的写法没有区别! & ...

  10. openshift上传java web项目

    下载当前客户端 OC(Openshift Client) https://mirror.openshift.com/pub/openshift-v3/clients/3.9.14/windows/oc ...