C # Start and close the process

Source: Internet
Author: User

The main content of this article is to close the process through code.

In the past, the process was shut down by using the BAT file. Now, you can close the process in another way.

The main idea of shutting down a process is to traverse all processes and identify the process to be shut down based on the process name.

Main Idea of enabling the process: Find all the EXE files in the folder and enable them recursively.

The main code is as follows:

1 # Region Method
2 /// <summary>
3 // close the application
4 /// </Summary>
5 // <Param name = "arrayprocessname"> separate application names with commas (,). </param>
6 private void closeapp (string arrayprocessname)
7 {
8 string [] processname = arrayprocessname. Split (',');
9 foreach (string appname in processname)
10 {
11 process [] localbynameapp = process. getprocessesbyname (appname); // obtain all processes with the program name
12 if (localbynameapp. length> 0)
13 {
14 foreach (VAR app in localbynameapp)
15 {
16 if (! App. hasexited)
17 {
18 app. Kill (); // close the process
19}
20}
21}
22}
23}
24
25 /// <summary>
26 // start the process
27 /// </Summary>
28 // <Param name = "arrayfolderpath"> you need to enable the path of the Process folder. Multiple paths are separated by commas (,). eg: D: \ test, E: \ Temp </param>
29 private void Startapp (string arrayfolderpath)
30 {
31 string [] foldersnamepath = arrayfolderpath. Split (',');
32 foreach (string foldernamepath in foldersnamepath)
33 {
34 getfolderapp (foldernamepath );
35}
36}
37
38 // <summary>
39 // recursively traverse all the EXE files in the folder. This method can be further extended to other suffix files.
40 /// </Summary>
41 // <Param name = "foldernamepath"> folder path </param>
42 private void getfolderapp (string foldernamepath)
43 {
44 string [] folderspath = directory. getdirectories (foldernamepath );
45 foreach (string folderpath in folderspath)
46 {
47 getfolderapp (folderpath );
48}
49
50 string [] filespath = directory. getfiles (foldernamepath );
51 foreach (string filepath in filespath)
52 {
53 fileinfo = new fileinfo (filepath );
54
55 // enable files suffixed with exe
56 If (fileinfo. extension. Equals (". EXE "))
57 {
58 process. Start (filepath );
59}
60}
61
62}
63 # endregion

The winform interface is as follows:

Download the source code here.

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.