brief introduction
When we use C ා to develop client programs, we inevitably need to call external programs or visit websites. This blog introduces three ways to call external applications for reference
Realization
The first is to use shell32.dll to implement ShellExecute method, which can open local program, folder or visit website at the same time. As long as you directly input path string, such as C:\Users\Desktop\xx.exe or 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 current window var wih = new WindowInteropHelper(wnd); //This class supports getting hWnd IntPtr hWnd = wih.Handle; //Get window handle var result = ShellExecute(hWnd, "open", "The path to be opened is as follows C:\Users\Desktop\xx.exe", null, null, (int)ShowWindowCommands.SW_SHOW);
[DllImport("shell32.dll")] public static extern IntPtr ShellExecute(IntPtr hwnd, //Window handle string lpOperation, //Specify what to do string lpFile, //Program to execute, folder to browse, or web address string lpParameters, //If the lpFile parameter is an executable, this parameter specifies the command line parameters string lpDirectory, //Specify default directory int nShowCmd //If the 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, //Display a window and make it active SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_MAX = 10 }
The second is to use kernel32.dll to implement the WinExec method. This method can only open the local program and judge whether the call is successful according to the return value (< 32 means there is an error)
var result = WinExec(pathStr, (int)ShowWindowCommands.SW_SHOW);
[DllImport("kernel32.dll")] public static extern int WinExec(string programPath, int operType);
The third method is to use the Process class. For the specific application of the Process class, you can see the definition of the class. Here, you can only use it to open files and visit websites. (for other usage methods, please refer to https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx ), 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 specific implementation 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 website
try { Process.Start("iexplore.exe", pathStr); } catch (Exception ex) { MessageBox.Show(ex.Message); }
It can be seen that it's easy for C to call external programs. Please leave a message if you have any supplementary information. Thank you
Demo Download
Link: https://pan.baidu.com/s/1G1PAneEJSCQR3hGpGpaDzQ
Password: rtew