SoFunction
Updated on 2025-03-07

Detailed explanation of opening and closing executable files in C#

1. Open the program

First introduce the class: used to start and stop processes.

Process pr = new Process();//Declare a process class object    = "E://Program Files//Tencent//QQ//";//Specify the physical path of my QQ to run.   ();//Run QQ

It can also be simpler:Process's static method Start();

(String fileName);(+4Reload) //filiName is the program name you want to run, it is the physical path(String fileName,string arguments)//filiName is the program name you want to run, it is the physical path; arguments passed command line parameters when starting the program change.

2. Take the QQ just now as an example to explain
1、

Process[] proc = ("QQ");//Create an array of processes and associate resources related to this process.   for (int i = 0; i < ; i++)
   {
    proc[i].Kill(); //End the process one by one.   }

2、

Process[] p_arry = ();//Get all processes in the system   for (int i = 0; i < p_arry.Length; i++)//Transfer each process   {
    if (p_arry[i].ProcessName == "QQ")//Discover a process called QQ    {     
     p_arry[i].Kill();// Just end it.     return;
    }
   }
   ();//Garbage recycling

This method is a bit resource-consuming, and if you have any judgment, you can try to use it.
III. Program

using System;
class test
{
static void Main()
{

//Declare a program information class Info = new ();

//Set external program name = "";

//Set the startup parameters (command line parameters) of the external program to = "";

//Set the working directory of the external program to C:\ = "C:\\";

//Declare a program class Proc ;

try
{
//
//Start external programs//
Proc = (Info);
}
catch(.Win32Exception e)
{
("The system cannot find the specified program file。\r{0}", e);
return;
}

//Print out the start execution time of the external program("The start execution time of external programs:{0}", );

//Wait for 3 seconds(3000);

//If this external program does not end, it will be forced to terminate itif( == false)
{
("The main program will forcefully terminate the operation of the external program!");
();
}
else
{
("Exit normally by external programs!");
}
("Exit run time of external program:{0}", );
("The return value of an external program at the end of the run:{0}", );
}
}

Taking the opening and closing of QQ applications as an example, we will help everyone better learn how to open and close executable files in C#. I hope it will be helpful to everyone's learning.