C# 调用可执行exe文件几种方法小结

文章介绍了在C#中使用进程池、遮蔽CLI启动窗口以及异步启动exe程序的代码示例。通过Process类的不同配置,实现不同需求的程序启动,包括参数传递、隐藏窗口和读取标准输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.利用进程池方式启动

string exefile = "xxx.exe";
if (File.Exists(exefile)) {
    Process process = new Process();   // params 为 string 类型的参数,多个参数以空格分隔,如果某个参数为空,可以传入””
    ProcessStartInfo startInfo = new ProcessStartInfo(exefile, params);
    process.StartInfo = startInfo;
    process.Start();
}

2.遮蔽CLI启动窗口

string exefile = "xxx.exe";
if (File.Exists(exefile)) {
    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo(exefile, path);
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;    startInfo.CreateNoWindow = true;
    process.StartInfo = startInfo;
    process.Start();
    process.WaitForExit(2000);
    string output = process.StandardOutput.ReadToEnd();
    rtb_analyze.Text = output;
    process.Close();         
}

3.异步启动方式

public void exec(string exePath, string parameters)
{
    Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = exePath;
    process.StartInfo.Arguments = parameters;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.BeginOutputReadLine();
    process.OutputDataReceived += new  DataReceivedEventHandler(processOutputDataReceived);
}

资料参考地址:http://t.csdn.cn/epXmh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值