当前位置:网站首页>C# 客户端程序调用外部程序的3种实现方法
C# 客户端程序调用外部程序的3种实现方法
2022-07-26 19:18:00 【biyusr】
这篇文章主要给大家介绍了关于 C# 客户端程序调用外部程序的 3 种实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值。
简介
大家都知道,当我们用 C# 来开发客户端程序的时候,总会不可避免的需要调用外部程序或者访问网站,本文介绍了三种调用外部应用的方法,供参考,下面话不多说了,来一起看看详细的介绍吧。
实现
第一种是利用 shell32.dll,实现 ShellExecute 方法,该方法可同时打开本地程序、文件夹或者访问网站,只要直接输入路径字符串即可, 如 C:\Users\Desktop\xx.exe 或者 https://cn.bing.com/,可以根据返回值判断是否调用成功 (成功0x00000002a , 失败0x00000002)
Window wnd = Window.GetWindow(this); //获取当前窗口var wih = new WindowInteropHelper(wnd); //该类支持获取hWndIntPtr hWnd = wih.Handle; //获取窗口句柄var result = ShellExecute(hWnd, "open", "需要打开的路径如C:\Users\Desktop\xx.exe", null, null, (int)ShowWindowCommands.SW_SHOW);
[DllImport("shell32.dll")]public static extern IntPtr ShellExecute(IntPtr hwnd, //窗口句柄string lpOperation, //指定要进行的操作string lpFile, //要执行的程序、要浏览的文件夹或者网址string lpParameters, //若lpFile参数是一个可执行程序,则此参数指定命令行参数string lpDirectory, //指定默认目录int nShowCmd //若lpFile参数是一个可执行程序,则此参数指定程序窗口的初始显示方式(参考如下枚举));
public enum ShowWindowCommands : int{SW_HIDE = 0,SW_SHOWNORMAL = 1,SW_NORMAL = 1,SW_SHOWMINIMIZED = 2,SW_SHOWMAXIMIZED = 3,SW_MAXIMIZE = 3,SW_SHOWNOACTIVATE = 4,SW_SHOW = 5, //显示一个窗口,同时令其进入活动状态SW_MINIMIZE = 6,SW_SHOWMINNOACTIVE = 7,SW_SHOWNA = 8,SW_RESTORE = 9,SW_SHOWDEFAULT = 10,SW_MAX = 10}
第二种是利用 kernel32.dll,实现 WinExec 方法,该方法仅能打开本地程序,可以根据返回值判断是否调用成功(<32表示出现错误)
var result = WinExec(pathStr, (int)ShowWindowCommands.SW_SHOW);[DllImport("kernel32.dll")]public static extern int WinExec(string programPath, int operType);
第三种方法是利用 Process 类,Process 类具体应用可以看类的定义,这里只实现它打开文件和访问网站的用法,调用失败会抛出异常
/// <devdoc>/// <para>/// Provides access to local and remote/// processes. Enables you to start and stop system processes./// </para>/// </devdoc>
具体实现为
//调用程序Process process = new Process();try{process.StartInfo.UseShellExecute = false;process.StartInfo.FileName = pathStr;process.StartInfo.CreateNoWindow = true;process.Start();}catch (Exception ex){MessageBox.Show(ex.Message);}//访问网站try{Process.Start("iexplore.exe", pathStr);}catch (Exception ex){MessageBox.Show(ex.Message);}
边栏推荐
- 【shell】转载:批量替换 find awk sed xargs
- Software testing - what are the automated testing frameworks?
- 移动端video兼容你需要知道的几点
- 企业内部信息碎片化该怎么办?不妨试试这样做
- BluePrism流程业务对象的组件功能介绍-RPA第三章
- MySQL subquery usage
- numpy.put()
- eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
- What should we do about the fragmentation of internal information? Try this
- 【实习经验】日期校验
猜你喜欢

靠元宇宙和NFT,天下秀疯狂“割韭菜”?

three.js 给地球加标签和弹窗

Digital transformation of enterprises has become a general trend, and it is important to choose the right online collaboration tools

How to uninstall win11 edge? The method tutorial of completely uninstalling win11 edge browser

eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c

以 NFT 为抵押品的借贷协议模式探讨

ShardingSphere-JDBC 关键字问题

使用ECS和OSS搭建个人网盘
![Design of intelligent weighing system based on Huawei cloud IOT (STM32) [i]](/img/e4/4ebce448debf4bae308e2d5972a2a2.png)
Design of intelligent weighing system based on Huawei cloud IOT (STM32) [i]

网络与VPC动手实验
随机推荐
EFCore Migrations的深入研究
three.js 制作地球标注的两种方法
FastTunnel-开源内网穿透框架
ShardingSphere-JDBC 关键字问题
Codeforces Round #810 (Div. 2)(A~C)
【Android】Kotlin 快速编译背后的黑科技,了解一下~
【ffmpeg】给视频文件添加时间戳 汇总
[PHP] save session data to redis
BluePrism流程业务对象的组件功能介绍-RPA第三章
Student‘s t分布
Summary of iPhone development data persistence (final) - Comparison of five data persistence methods
靠元宇宙和NFT,天下秀疯狂“割韭菜”?
记一次 .NET 某物管后台服务 卡死分析
徽商期货开户安全吗?请问徽商期货开户应该注意什么呢?
DevSecOps,让速度和安全兼顾
[PHP] MySQL native PHP operation - Tianlong eight steps
金仓数据库 KingbaseES SQL 语言参考手册 (21. KES正则表达式支持)
【二叉树】将二叉搜索树变平衡
【OBS】Dropped Frames And General Connection Issues
kvm虚拟化