当前位置:网站首页>Three implementation methods of C # client program calling external program
Three implementation methods of C # client program calling external program
2022-07-23 10:48:00 【biyusr】
This article mainly introduces about C# The client program calls the... Of the external program 3 There are ways to do it , In this paper, the example code is introduced in detail , It has certain reference learning value for everyone's study or work .
brief introduction

Everybody knows , When we use C# To develop client programs , It's inevitable to call external programs or visit websites , This paper introduces three methods of calling external applications , For reference , I don't want to say much next , Let's have a look at the detailed introduction .
Realization

The first is to use shell32.dll, Realization ShellExecute Method , This method can open the local program at the same time 、 Folder or visit website , Just enter the path string directly , Such as C:\Users\Desktop\xx.exe perhaps https://cn.bing.com/, You can judge whether the call is successful according to the return value ( success 0x00000002a , Failure 0x00000002)
Window wnd = Window.GetWindow(this); // Get the current windowvar wih = new WindowInteropHelper(wnd); // This kind of support obtains hWndIntPtr hWnd = wih.Handle; // Get window handlevar result = ShellExecute(hWnd, "open", " The path that needs to be opened, such as C:\Users\Desktop\xx.exe", null, null, (int)ShowWindowCommands.SW_SHOW);
[DllImport("shell32.dll")]public static extern IntPtr ShellExecute(IntPtr hwnd, // Window handlestring lpOperation, // Specify the operation to be performedstring lpFile, // The program to be executed 、 Folder or web address to browsestring lpParameters, // if lpFile Parameter is an executable program , This parameter specifies the command line parameterstring lpDirectory, // Specify the default directoryint nShowCmd // if lpFile Parameter is an executable program , This parameter specifies the initial display mode of the program window ( Refer to the following enumeration ));
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, // Show a window , At the same time, make it activeSW_MINIMIZE = 6,SW_SHOWMINNOACTIVE = 7,SW_SHOWNA = 8,SW_RESTORE = 9,SW_SHOWDEFAULT = 10,SW_MAX = 10}
The second is to use kernel32.dll, Realization WinExec Method , This method can only open local programs , You can judge whether the call is successful according to the return value (<32 Indicates an error occurred )
var result = WinExec(pathStr, (int)ShowWindowCommands.SW_SHOW);[DllImport("kernel32.dll")]public static extern int WinExec(string programPath, int operType);
The third way is to use Process class ,Process The specific application of class can be seen from the definition of class , Here we only realize the usage of opening files and accessing websites , If the call fails, an exception will be thrown
/// <devdoc>/// <para>/// Provides access to local and remote/// processes. Enables you to start and stop system processes./// </para>/// </devdoc>
The concrete realization is // Calling procedure
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);}// Visit websitetry{Process.Start("iexplore.exe", pathStr);}catch (Exception ex){MessageBox.Show(ex.Message);}
边栏推荐
- 云徙科技CTO李楠:技术和技术人,与数字化共同进化
- TZC 1283: simple sort - heap sort
- 构建人工智能产品/业务的两种策略(by Andrew Ng)
- 0 basic career change software test, the necessary skills with a monthly salary of 6000 and 11000 are quite different
- Question 300 Leçon 6 type quadratique
- thing JS笔记
- Interest rate in installment payment
- MySQL index operation
- 300 题 第六讲 二次型
- DPDK 交叉编译基本流程
猜你喜欢

FPGA——SPI总线控制flash(2)(含代码)

12 个适合做外包项目的开源后台管理系统

Operator usage and scheduling process of 31 spark

PXE remote installation and kickstart unattended installation technical documents

JMeter record the BeanShell written into excel instance caused by an automatic data generation

China Economic Net: "Yuan universe" is hot

Introduction to partition operators, broadcast variables and accumulators of 32 spark

数据湖:Apache Iceberg介绍

Rapid SQL all platforms high performance SQL code

PowerBI入门指南
随机推荐
常见神经网络参数量与计算量
Important knowledge of application layer (interview, reexamination, term end)
编译构建工具-bazel
Niuke brush questions - Sword finger offer (phase II)
04_ UE4 advanced_ Introduction to physical collision and firing fireballs
SQLZOO——SELECT from WORLD Tutorial
0基础转行软件测试,月薪6000和11000的必备技能,截然不同...
写驱动程序的时候warning LNK4210报错
低代码平台搭建医药企业供应商、医院、患者等多方协同管理案例分析
PowerBI入门指南
Network data leakage events occur frequently, how to protect personal privacy information?
Kubernetes technology and Architecture (VI)
SVG、canvas、绘制线段和填充多边形、矩形、曲线的绘制和填充
Custom events in components
thing JS笔记
PMP每日一练 | 考试不迷路-7.22
【Swift|Bug】Xcode提示Error running playground: Failed to prepare for communication with playground
TZC 1283: 简单排序 —— 堆排序
toco生成tflite模型
[unity daily bug] unity reports an unexpected character '‘