c#启动关闭进程

以前关闭进程的方式,通常采用bat文件的方式。现在通过采用另外一种方式关闭进程。

关闭进程主要思路:遍历所有进程,根据进程名称,找出需要关闭的进程。

开启进程主要思路:通过递归的方式找出文件夹中所有的exe文件,并且开启。

其主要代码如下:

 

1 #region 方法
2 /// <summary>
3 /// 关闭应用程序
4 /// </summary>
5 /// <param name="ArrayProcessName">应用程序名之间用‘,’分开</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);//获取程序名的所有进程
12 if (localByNameApp.Length > 0)
13 {
14 foreach (var app in localByNameApp)
15 {
16 if (!app.HasExited)
17 {
18 app.Kill();//关闭进程
19 }
20 }
21 }
22 }
23 }
24
25 /// <summary>
26 /// 开启进程
27 /// </summary>
28 /// <param name="ArrayFolderPath">需要开启进程文件夹的路径,多个路径用‘,’隔开;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 /// 递归遍历文件夹内所有的exe文件,此方法可以进一步扩展为其它的后缀文件
40 /// </summary>
41 /// <param name="folderNamePath">文件夹路径</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 fileInfo = new FileInfo(filePath);
54
55 //开启后缀为exe的文件
56 if (fileInfo.Extension.Equals(".exe"))
57 {
58 Process.Start(filePath);
59 }
60 }
61
62 }
63 #endregion

winform的界面如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章