当我们用 c# 来开发客户端程序的时候,总会不可避免的需要调用外部程序或者访问网站
方式一:直接调用process类来实现:
using system.diagnostics; process p = process.start("program.exe"); p.waitforexit();//本行代码不是必须,但是很关键,限制等待外部程序退出后才能往下执行
当exe程序需要传递参数时:
using system.diagnostics; process p = process.start("program.exe 参数"); p.waitforexit();//本行代码不是必须,但是很关键,限制等待外部程序退出后才能往下执行
方式二:利用shell32.dll,调用shellexecute方法
shellexecute 方法可以打开一个已注册的文件、打开一个目录、打印一个文件等等,可以根据返回值判断是否调用成功。函数如下:
intptr shellexecute(intptr hwnd, string lpoperation, string lpfile, string lpparameters, string lpdirectory, showcommands nshowcmd);
其中,各参数信息如下:
- hwnd:指定父窗口句柄,未指定时可以为 null 或者为 0
- lpoperation:指定操作,值可以为【open】、【print】、【explore】。open:执行由 lpfile 参数指定的程序,或打开由lpfile 参数指定的文件或文件夹;print :打印由 lpfile 参数指定的文件;explore:浏览由lpfile参数指定的文件夹。当参数设为 null 时,默认为 open。
- lpfile:指定要打开的文件或程序
- lpparameters:给要打开的程序指定参数;如果打开的是文件,值为 null
- lpdirectory:默认目录
- nshowcmd:打开设置项,具体意义如下:
sw_hide = 0; //隐藏 sw_shownormal = 1; //用最近的大小和位置显示, 激活 sw_normal = 1; //同 sw_shownormal sw_showminimized = 2; //最小化, 激活 sw_showmaximized = 3; //最大化, 激活 sw_maximize = 3; //同 sw_showmaximized sw_shownoactivate = 4; //用最近的大小和位置显示, 不激活 sw_show = 5; //同 sw_shownormal sw_minimize = 6; //最小化, 不激活 sw_showminnoactive = 7; //同 sw_minimize sw_showna = 8; //同 sw_shownoactivate sw_restore = 9; //同 sw_shownormal sw_showdefault = 10; //同 sw_shownormal sw_max = 10; //同 sw_shownormal
返回值说明:返回值大于 32 时,即执行成功。执行失败的返回值具体意义如下:
0 = 0 //内存不足 error_file_not_found = 2; //文件名错误 error_path_not_found = 3; //路径名错误 error_bad_format = 11; //exe 文件无效 se_err_share = 26; //发生共享错误 se_err_associncomplete = 27; //文件名不完全或无效 se_err_ddetimeout = 28; //超时 se_err_ddefail = 29; //dde 事务失败 se_err_ddebusy = 30; //正在处理其他 dde 事务而不能完成该 dde 事务 se_err_noassoc = 31; //没有相关联的应用程序
代码实现如下:
// 引入库 [dllimport("shell32.dll")] static extern intptr shellexecute(intptr hwnd, string lpoperation, string lpfile, string lpparameters, string lpdirectory, showcommands nshowcmd); // 调用 string filepath = @"c:\users\administrator\desktop\program.exe"; intptr result = shellexecute(intptr.zero, "open", filepath, "", "", showcommands.sw_shownormal);
方式三:kernel32.dll,调用winexec方法
winexec方法仅能打开本地程序,可以根据返回值判断是否调用成功(<32表示出现错误)。
uint winexec(exepath,showcmd)
参数说明:
–xepath:命令行参数。注意,要用pchar转化一下。
–showcmd:外部程序的运行方式。其取值如下:
----sw_hide 隐藏
----sw_maximize 最大化
----sw_minimize 最小化,并把zorder顺序在此窗口之后(即窗口下一层)的窗口激活
----sw_restore 激活窗口并还原为初始化大小 sw_show以当前大小和状态激活窗口
----sw_show 用当前的大小和位置显示一个窗口,同时令其进入活动状态
----sw_showdefault 以默认方式运行
----sw_showmaximized 激活窗口并最大化
----sw_showminimized 激活窗口并最小化
----sw_showminnoactive最小化但不改变当前激活的窗口
----sw_showna 以当前状态显示窗口但不改变当前激活的窗口
----sw_shownoactivate 以初始化大小显示窗口但不改变当前激活的窗口
----sw_shownormal 激活并显示窗口,如果是最大(小)化,窗口将会还原。第一次运行程序 时应该使用这个值
如果调用成功,这个函数会返回一个大于等于32的值,否则调用失败,其返回值的意义如下:
–0 系统内存或资源不足
–error_bad_format .exe文件格式无效(比如不是32位应用程序)
–error_file_not_found 指定的文件设有找到
–error_path_not_found 指定的路径没有找到
// 引入库 [dllimport("kernel32.dll")] public static extern int winexec(string programpath, int opertype); // 调用 string pathstr=@"c:\users\administrator\desktop\program.exe"; var result = winexec(pathstr, (int)showwindowcommands.sw_show);
c# 调用可执行exe文件几种方法小结
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); }
参考资料:
1、
2、https://www.cnblogs.com/daochangone/p/11364775.html
3、https://www.cnblogs.com/semth/p/10494685.html
到此这篇关于c# 调用外部exe的三种方式的文章就介绍到这了,更多相关c# 调用外部exe内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论