Three implementations of the C # client program calling external programs

Source: Internet
Author: User

Brief introduction

When we use C # to develop client programs, it is inevitable to call external programs or visit the site, this blog introduces three ways to invoke external applications for reference

Realize

The first is to use Shell32.dll to implement the ShellExecute method, which can open a local program, a folder, or access a Web site at the same time, just enter the path string directly, such as C:\Users\Desktop\xx.exe or https:// cn.bing.com/, you can determine whether the call succeeds based on the return value (Success 0x00000002a, failure 0x00000002)

Window wnd = Window.getwindow (this);    Gets the current window varnew= wih. Handle;               Gets the window handle var" Open" needs to open the path such as C:\Users\Desktop\xx.exe  "nullnull, (int) showwindowcommands.sw_show);
[DllImport ("Shell32.dll")] Public Static externIntPtr ShellExecute (INTPTR hwnd,//window handlestringlpoperation,//Specify the action to be madestringlpfile,//Programs to execute, folders to browse, or URLsstringlpparameters,//If the Lpfile parameter is an executable program, this parameter specifies the command-line argumentsstringlpdirectory,//Specify default directoryintnShowCmd//If the Lpfile parameter is an executable program, this parameter specifies how the program window is initially displayed (see the enumeration below));
 Public enumShowwindowcommands: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,//displays a window and moves it to the active stateSw_minimize =6, Sw_showminnoactive=7, Sw_showna=8, Sw_restore=9, Sw_showdefault=Ten, Sw_max=Ten}

The second is to use Kernel32.dll to implement the WinExec method, which can only open the local program, can determine whether the call succeeds based on the return value (the <32 indicates that an error occurred)

var result = WinExec (PATHSTR, (int) showwindowcommands.sw_show);
[DllImport ("Kernel32.dll")]  Public Static extern int WinExec (stringint opertype);

The third method is to use the process class, the Process class specific application can see the definition of the class, here only to open the file and access to the site's usage, (other ways to use refer to https://msdn.microsoft.com/en-us/library/ System.Diagnostics.Process (v=vs.110). aspx), calling failure throws an exception

/// <devdoc> ///    <para> /// //// </para>//</                     Devdoc>

Specifically implemented as

//Calling program 
Process process = new Proce SS (); try {process. Startinfo.useshellexecute = false ; Process. Startinfo.filename = Pathstr; Process. Startinfo.createnowindow = true ; Process. Start (); catch (Exception ex) {messagebox.sh Ow (ex. Message); }
Visit website
Try { Process.Start ("iexplore.exe", Pathstr);} Catch (Exception ex) { MessageBox.Show (ex). Message);}

As you can see, C # calls external programs or is relatively easy ~ There are complementary places to welcome the message, thank you

Demo download

Links: Https://pan.baidu.com/s/1G1PAneEJSCQR3hGpGpaDzQ

Password: Rtew

Three implementations of the C # client program calling external programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.