c#中如何启用外部exe程序?

有哪位高人指点一下 在c#中如何调用外部的exe程序?
2025-04-26 19:13:49
推荐回答(4个)
回答1:

c#调用外部exe程序,首先要
using System.Diagnostics;

然后开启一个新process
System.Diagnostics.ProcessStartInfo p=null;
System.Diagnostics.Process Proc;

p = new ProcessStartInfo("nnnn.exe","参数");
p.WorkingDirectory = exepath;//设置此外部程序所在windows目录
Proc = System.Diagnostics.Process.Start(p);//调用外部程序

回答2:

两种方法
(一)
打开项目属性,选择调试选项卡,将“启用非托管代码调试”一项钩上。
打开项目属性,选择调试选项卡,将“启用Visual Studio宿主进程“一项钩掉。

(二)
Terminal Services 被禁用,直接启动该服务即可解决问题。

回答3:

Process.Start(@"C:\Documents and Settings\Administrator\桌面\a.exe");

回答4:

这个很 EASY 的,帮你百度一下:

// 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,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

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

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

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

void proc_Exited(object sender, EventArgs e)
{
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
总结了一下常用的属性
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();