Call another application or command line (. exe with parameters) in C #

Source: Internet
Author: User

Using system.diaglostics.Process in. NET can be used to invoke another command line or program.

Using System.Diagnostics;
If it's a DOS
Process.Start ("cmd.exe");
If it is another file
Process.Start ("absolute path + filename. exe");
------------------------------------
How to call an external DOS program in C #.
Working with Process objects:
System.Diagnostics.Process p=new System.Diagnostics.Process ();
P.startinfo.filename= "Arj.exe";//The name of the program to start
p.startinfo.arguments= "-X Sourcefile.arj c:/temp";//Startup parameters
P.start ()/Start
if (p.hasexisted)//To determine whether the run ends
P.kill ();



--------------------------------------------------------------------------------------------------------------- ----------------------
<summary>
To start another application
</summary>
<param name= "File" > Application name </param>
<param name= "Workdirectory" > Application working directory </param>
<param name= "args" > Command line Arguments </param>
<param name= "style" > Window style </param>
public static bool StartProcess (string file,string workdirectory,string Args,processwindowstyle style)
{
Try
{
Process myprocess = new process ();
ProcessStartInfo startinfo = new ProcessStartInfo (File,args);
Startinfo.windowstyle = style;
Startinfo.workingdirectory = workdirectory;
Myprocess. StartInfo = StartInfo;
Myprocess. Startinfo.useshellexecute = false;
Myprocess. Start ();
return true;
}
catch (Exception E0)
{
MessageBox.Show ("An error occurred while starting the application. Reason: "+ E0." message);
}
return false;
}



String parms = "" + globalobject.getinstance (). UserID + "" + globalobject.getinstance (). UserPassword;
if (publicmethods.startprocess (Application.startuppath + @ "/uptool/uptool.exe", Application.startuppath + "//UpTo") Ol ", Parms,processwindowstyle.normal))
{
Environment.exit (0);
}
--------------------------------------------------------------------------------------------------------------- -------
Process.Start ("IExplore.exe", "http://www.newhappy.cn");
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo ();
Startinfo.filename = "executable exe filename";
startinfo.arguments = "parameter array";
System.Diagnostics.Process.Start (StartInfo);
--------------------------------------------------------------------------------------------------------------- -------------
1. There are a lot of times, we need to call the external EXE program, and we have to wait for it to run, we can continue the following action, then how do we achieve, please see the following code.
' How to wait for the external program to complete.
' Read files from System Folder
Dim Sysfolder as String = _
Environment.getfolderpath (Environment.SpecialFolder.System)
' Create a new process structure
Dim pinfo as New ProcessStartInfo ()
' Set its member filename to the system data Eula.txt
pInfo.FileName = sysfolder & "/eula.txt"
' Run the file
Dim p as Process = Process.Start (pinfo)
' Wait for program load to complete
P.waitforinputidle ()
' Waiting for the trip to exit
p.WaitForExit ()
' Continue to execute the following code
MessageBox.Show ("Continue executing code")


2. We want to force it off after 5 seconds. Instead of needing me to hand it off.
' Set exit time
Dim TimeOut as Integer = 5000
Dim Sysfolder as String = _
Environment.getfolderpath (Environment.SpecialFolder.System)
Dim pinfo as New ProcessStartInfo ()
pInfo.FileName = sysfolder & "/eula.txt"
Dim p as Process = Process.Start (pinfo)
P.waitforinputidle ()
p.WaitForExit (TimeOut)
' Check to see if it has been closed before timeout.
If p.hasexited = False Then
' The process is still running
' See if the process is responding.
If p.responding Then
P.closemainwindow () ' Close window '
Else
P.kill () ' forcibly interrupted
End If
End If
MessageBox.Show ("Continue executing code")

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.