Explanation of C opening and closing executable files
- 2021-08-31 08:55:10
- OfStack
Step 1 Open the program
First, introduce the System. Diagnostics. Process classes: used to start and stop processes.
Process pr = new Process();// Declaration 1 Process class objects
pr.StartInfo.FileName = "E://Program Files//Tencent//QQ//QQ.exe";// Specify the program to run, my QQ Gets or sets the physical path of the.
pr.Start();// Run QQ
It can also be simple: Start (), the static method of Process;
Process.Start(String fileName);(+4 Heavy load ) //filiName Is the name of the program you want to run, is the physical path
Process.Start(String fileName,string arguments)//filiName Is the name of the program you want to run, is the physical path ;arguments Command line parameters passed when starting the modification program
2. Take QQ just now as an example to explain
1.
Process[] proc = Process.GetProcessesByName("QQ");// Create 1 An array of processes, associating resources related to this process.
for (int i = 0; i < proc.Length; i++)
{
proc[i].Kill(); // End the process one by one .
}
2.
Process[] p_arry = Process.GetProcesses();// Get all the processes in the system
for (int i = 0; i < p_arry.Length; i++)// Traverse each process
{
if (p_arry[i].ProcessName == "QQ")// Found a name called QQ The process of
{
p_arry[i].Kill();// Just end it.
return;
}
}
System.GC.Collect();// Garbage collection
This method is a little resource-consuming, and there is no if judgment on all kill, you can try to use it.
3. Procedures
using System;
class test
{
static void Main()
{
// Declaration 1 Program information classes
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
// Set the external program name
Info.FileName = "notepad.exe";
// Set the startup parameters (command line parameters) of external programs to test.txt
Info.Arguments = "test.txt";
// Set the external program working directory to C:\
Info.WorkingDirectory = "C:\\";
// Declaration 1 Program classes
System.Diagnostics.Process Proc ;
try
{
//
// Start an external program
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception e)
{
Console.WriteLine(" The system cannot find the specified program file. \r{0}", e);
return;
}
// Print out the starting execution time of the external program
Console.WriteLine(" Start execution time of external program: {0}", Proc.StartTime);
// Wait 3 Seconds
Proc.WaitForExit(3000);
// Forcibly terminate this external program if it does not end running
if(Proc.HasExited == false)
{
Console.WriteLine(" The main program forcibly terminates the operation of external programs! ");
Proc.Kill();
}
else
{
Console.WriteLine(" Exit normally by external program! ");
}
Console.WriteLine(" End runtime of external program: {0}", Proc.ExitTime);
Console.WriteLine(" The return value of the external program at the end of the run: {0}", Proc.ExitCode);
}
}
To open and close the QQ application for example to learn, to help you better learn C # implementation of open and close the executable method, I hope to help you learn.