C#调用外部exe

C#调用外部exe

        在C#Windows窗体应用程序的开发过程中,比如说要做成一个大的系统,集成很多其他应用的功能,可以利用现有的可运行的exe程序来丰富并完善系统。

1、调用外部exe(无传入参数)

在这里插入图片描述

QQ.exe

1.1 cmd方式调用QQ.exe(无传入参数)

        首先进入到C:\Program Files (x86)\Tencent\QQ\Bin文件夹,在右侧空白处按住Shift键并点击鼠标右键,选择在此处打开命令行窗口后,用键盘输入QQ.exeEnter键即可运行QQ程序,如下图所示。

在这里插入图片描述
在这里插入图片描述

1.2 C#模拟cmd来调用外部exe(无传入参数)

        打开Visual Studio 2015,新建C#->Windows窗体应用程序,项目名称输入InvokeQQ后确定,进入项目后会自动生成一个Form1窗体。
在这里插入图片描述
在这里插入图片描述
        点开Form1窗体左侧的工具箱,将Button按钮拖入到Form1窗体中,然后在Button上右键选择属性,修改Button按钮的Text名称为启动外部QQ,并双击按钮进入按钮的点击触发事件函数button1_Click,在函数中输入三行代码来调用外部的QQ.exe程序,同时引入对应库,

using System.Diagnostics;//和其他的using放在一起
Process m_Process = new Process();
m_Process.StartInfo.FileName = "C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe";//QQ.exe所在的文件夹全路径
m_Process.Start();

    
    

在这里插入图片描述
在这里插入图片描述
        然后点击上方的启动按钮运行项目,项目运行后会打开Form1窗体,用鼠标点击窗体中的按钮会弹出QQ应用,运行结果如下图所示。
在这里插入图片描述

2、调用外部exe(需传入参数)

2.1 新建C++项目生成需传入参数的exe程序

在这里插入图片描述
        打开Visual Studio 2015,新建Visual C+±>Win 32控制台应用程序,输入项目名称为WithParameterApp后点击确定,在弹出的窗口中附加选项选择空项目,生成项目后在右侧解决方案下WithParameterApp项目下的源文件上右键点击添加->新建项,选择C++文件,输入名称为main.cpp。将以下代码复制到main.cpp中后运行即可在对应文件夹中生成需传入参数WithParameterApp.exe程序,这个程序是用来求两个数的和,VS中运行后结果如下图所示(默认为1个参数)。

#include <iostream>
using namespace std;
/*** 将字符串转成int ***/
int char2int(const char* str) {
	const char* p = str;
	bool neg = false;
	int res = 0;
	if (*str == '-' || *str == '+') {
		str++;
	}
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">*</span>str <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">*</span>str <span class="token operator">&lt;</span> <span class="token string">'0'</span> <span class="token operator">||</span> <span class="token operator">*</span>str <span class="token operator">&gt;</span> <span class="token string">'9'</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
		<span class="token keyword">break</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	res <span class="token operator">=</span> res <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token operator">*</span>str <span class="token operator">-</span> <span class="token string">'0'</span><span class="token punctuation">;</span>
	str<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">*</span>p <span class="token operator">==</span> <span class="token string">'-'</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
	res <span class="token operator">=</span> <span class="token operator">-</span>res<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> res<span class="token punctuation">;</span>

}
void main(int argc, char *argv[])
{
int cc=0;
std::cout << “总共输入” << argc << “个参数” << endl;
for(int i=0;i<argc;i++)
std::cout << argv[i] << endl;
if(argc ==3)
std::cout << “两个数的和为” << char2int(argv[1]) + char2int(argv[2]) << endl;
system(“pause”);
}

在这里插入图片描述
在这里插入图片描述
        如上图所示,D:\Visual Studio 2015\MyProjects\WithParameterApp\Debug文件夹下已经生成了WithParameterApp.exe

2.1.1 cmd调用外部exe(传入参数)

        在D:\Visual Studio 2015\MyProjects\WithParameterApp\Debug文件夹下打开cmd窗口,输入以下命令后回车:WithParameterApp.exe 456 789
在这里插入图片描述
        可以看到,运行结果中显示了456和789两个数的和为1245.

2.1.2 C#模拟cmd调用外部exe(传入参数)

        在上述项目名称为InvokeQQ的C# Windows窗体应用程序中在Form1窗体中再添加一个Button,Button的Text属性改为InvokeWithParameterApp,并双击该Button,在Button的Click事件函数中输入以下代码来进行外部exe的调用(传入参数),然后点击上方的运行绿色按钮,结果如下图所示。

Process cmd = new Process(); 
cmd.StartInfo.FileName = @"D:\Visual Studio 2015\MyProjects\WithParameterApp\Debug\WithParameterApp.exe";
cmd.StartInfo.WorkingDirectory = @"D:\Visual Studio 2015\MyProjects\WithParameterApp\Debug\"; 
cmd.StartInfo.CreateNoWindow = false;//显示命令行窗口
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
string param1 = "456", param2 = "789";
cmd.StartInfo.Arguments = param1+" "+param2;
cmd.Start();

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

2.2 调用外部exe(传入带不同路径的参数)

2.2.1 cmd调用外部Python脚本.py(传入带不同路径的参数)

python.exe
在这里插入图片描述

algorithm123.py py脚本文件中注释也不要带中文最好,可有效避免编码解码错误

#-*- coding:utf-8 -*-
import sys
from os.path import exists

print(sys.argv[1])
print(sys.argv[2])
from_name = sys.argv[1]
to_name = sys.argv[2]

source=open(from_name)
indate=source.read()
print(indate)
source.close()

target=open(to_name,‘w’)
target.write(indate)
target.close()

在这里插入图片描述
在这里插入图片描述
        Win+R打开运行窗口,输入cmd打开命令行窗口后输入如下命令即可实现利用D:\Anaconda3\python.exe来执行E:\algorithm123\algorithm123.py脚本,然后将E:\异常\异常.txt文件复制到E:\algorithm123ExeResult\123456.txt

D:\Anaconda3\python.exe E:\algorithm123\algorithm123.py E:\异常\异常.txt E:\algorithm123ExeResult\123456.txt

 
 
  • 1

在这里插入图片描述

2.2.2 C#模拟cmd调用外部Python脚本.py(传入带不同路径的参数)

        在上述项目名称为InvokeQQ的C# Windows窗体应用程序中在Form1窗体中再次添加一个Button,Button的Text属性改为Invokealgorithm123,并双击该Button,在Button的Click事件函数中输入以下代码来进行外部exe的调用(传入带不同路径的参数),然后点击上方的运行绿色按钮,结果如下图所示。

Process cmd = new Process(); 
cmd.StartInfo.FileName = @"D:\Anaconda3\python.exe";//必要时可将python环境设置到环境变量Path的最前面
cmd.StartInfo.UseShellExecute = false; //此处必须为false否则引发异常
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.WorkingDirectory = "E:\\algorithm123\\";
cmd.StartInfo.CreateNoWindow = true;//不显示命令行窗口
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string path1 = "E:\\algorithm123\\algorithm123.py",path2= "E:\\异常\\异常.txt", path3= "E:\\algorithm123ExeResult\\12345678.txt";
cmd.StartInfo.Arguments = path1+" "+path2+" "+path3;
cmd.Start(); //启动进程
MessageBox.Show("文件复制成功!");

在这里插入图片描述
在这里插入图片描述

2.3 C#模拟cmd调用外部Python打包封装好的exe(传入带不同路径的参数)

2.3.1 利用pyinstaller将py脚本文件打包为可执行exe(传入带不同路径的参数)

        首先可以利用conda新建一个虚拟环境conda create --name mypython python=3.6,配置好清华镜像后就可以下载安装依赖包了,conda activate mypython进入虚拟环境,然后pip list查看已安装的包,由于py脚本文件可以通过pyinstaller来将现有的py文件打包为exe可执行程序。所以还要安装两个包pip install pyinstallerpip insatll requests,安装好的包列表如下图所示。
在这里插入图片描述

在这里插入图片描述
        下面利用Pyinstaller来将.py打包成exe,为了打包方便,我将虚拟环境里的python设置到了系统环境变量Path中,就是将D:\Anaconda3\envs\mypython和D:\Anaconda3\envs\mypython\Scripts放到了Path变量值得最前面,确定后再cmd中测试python和pip是否安装配置成功,如下图所示。
在这里插入图片描述
在这里插入图片描述
        然后在algorithm123.py脚本文件所在的E:\algorithm123文件夹下打开cmd后,输入如下命令:pyinstaller -F algorithm23.py,运行后提示打包成为exe成功,同时在文件夹下多了—pycache__、build和dist文件夹以及algorithm123.spec文件,其中在dist里有打包后的algorithm123.exe

在这里插入图片描述
在这里插入图片描述

2.3.2 cmd调用外部py脚本打包好的exe(传入带路径的参数)

        现在打开dist文件夹,并在此文件夹下打开cmd命令行窗口来测试打包后的algorithm123.exe能否运行成功,输入以下命令:algorithm123.exe E:\异常\异常.txt E:\algorithm123ExeResult\123456789exe.txt,cmd窗口显示正确的执行结果,同时在E:\algorithm123ExeResult文件夹下也成功生成了123456789exe.txt文件,如下图所示。
在这里插入图片描述
在这里插入图片描述

2.3.3 C#模拟cmd调用外部py脚本打包好的exe(传入带路径的参数)

        在上述项目名称为InvokeQQ的C# Windows窗体应用程序中在Form1窗体中再次添加一个Button,Button的Text属性改为Invokealgorithm123.exe,并双击该Button,在Button的Click事件函数中输入以下代码来进行外部py脚本打包exe的调用(传入带不同路径的参数),然后点击上方的运行绿色按钮,运行结果如下图所示。

Process cmd = new Process();
cmd.StartInfo.FileName = @"E:\algorithm123\dist\algorithm123.exe";
cmd.StartInfo.UseShellExecute = false; //此处必须为false否则引发异常
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.WorkingDirectory = "E:\\algorithm123\\dist\\";
cmd.StartInfo.CreateNoWindow = true;//不显示命令行窗口
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string path1 = "E:\\异常\\异常.txt", path2 = "E:\\algorithm123ExeResult\\123456exe.txt";
cmd.StartInfo.Arguments = path1 + " " + path2;
cmd.Start();
MessageBox.Show("调用外部Python脚本打包exe执行文件复制成功!"

在这里插入图片描述
在这里插入图片描述

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树预备知识模块管理 79681 人正在系统学习中
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值