Introduction
As we all know, when we use C# to develop client programs, we will inevitably need to call external programs or access the website. This blog introduces three methods of calling external applications for reference. I won’t say much below, let’s take a look at the detailed introduction together.
accomplish
The first is to use it to implement the ShellExecute method, which can open local programs, folders or access the website at the same time. Just enter the path string directly. For example, C:\Users\Desktop\ or /, you can judge whether the call is successful based on the return value (success 0x00000002a, failure 0x00000002)
Window wnd = (this); //Get the current windowvar wih = new WindowInteropHelper(wnd); //This class supports getting hWndIntPtr hWnd = ; //Get window handlevar result = ShellExecute(hWnd, "open", "The path that needs to be opened is like C:\Users\Desktop\", null, null, (int)ShowWindowCommands.SW_SHOW);
[DllImport("")] public static extern IntPtr ShellExecute(IntPtr hwnd, //Window handle string lpOperation, //Specify the operation to be performed string lpFile, //The program to be executed, the folder to be browsed or the URL to be browsed string lpParameters, //If the lpFile parameter is an executable program, this parameter specifies the command line parameter string lpDirectory, //Specify the default directory int nShowCmd //If the lpFile parameter is an executable program, this parameter specifies the initial display method of the program window (refer to the enumeration below) );
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 and make it active at the same time SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_MAX = 10 }
The second is to use it to implement the WinExec method. This method can only open the local program. You can judge whether the call is successful based on the return value (<32 means an error occurs)
var result = WinExec(pathStr, (int)ShowWindowCommands.SW_SHOW);
[DllImport("")] public static extern int WinExec(string programPath, int operType);
The third method is to use the Process class. The specific application of the Process class can be used to see the definition of the class. Here we only implement the usage of opening files and accessing websites (for other usage methods, please refer to /en-us/library/(v=vs.110).aspx). An exception will be thrown if the call fails.
/// <devdoc> /// <para> /// Provides access to local and remote /// processes. Enables you to start and stop system processes. /// </para> /// </devdoc>
Detailed implementation as
//Calling the program Process process = new Process(); try { = false; = pathStr; = true; (); } catch (Exception ex) { (); } // Visit the websitetry { ("", pathStr); } catch (Exception ex) { (); }
It can be seen that it is relatively easy to call external programs in C#~ If there are any additional places, please leave a message, thank you
Demo download:Click here
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.