进程管理

 

  C#进程管理

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.IO;
15 using System.Diagnostics;
16 
17 namespace jincheng
18 {
19     /// <summary>
20     /// MainWindow.xaml 的交互逻辑
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         public MainWindow()
25         {
26             InitializeComponent();
27         }
28         public int i = 1;
29         public string filename = "Notepad";
30         List<Data> list = new List<Data>();
31         private void button1_Click(object sender, RoutedEventArgs e)
32         {
33             string path = Environment.CurrentDirectory + "\\myfile" + (i++) + ".txt";
34             if (File.Exists(path ) == false)
35             {
36                 File.CreateText(path);
37             }
38             Process p = new Process();
39             p.StartInfo.FileName = filename;
40             p.StartInfo.Arguments = path;
41             p.Start();
42             Refresh();
43         }
44         public void Refresh()
45         {
46             dataGrid1.ItemsSource = null;
47             list.Clear();
48             Process[] ps = Process.GetProcessesByName(filename);
49             foreach (var item in ps)
50             {
51                 try
52                 {
53                     list.Add(new Data()
54                     {
55 
56                         Id = item.Id,
57                         ProcessName = item.ProcessName,
58                         TotalMemory = string.Format("{0}KB", item.WorkingSet64 / 1024d),
59                         StartTime = item.StartTime.ToString("yyyy-M-d HH:mm:ss"),
60                         FileName = item.MainModule.FileName
61 
62                     });
63                 }
64                 catch
65                 {
66                     break;
67                 }
68             }
69             dataGrid1.ItemsSource = list;
70  
71         }
72         public class  Data
73         {
74             public int Id {get;set;}
75             public string ProcessName { get; set; }
76             public string TotalMemory { get; set; }
77             public string StartTime { get; set; }
78             public string FileName { get; set; }
79         }
80 
81         private void button2_Click(object sender, RoutedEventArgs e)
82         {
83             Process[] pss = Process.GetProcessesByName(filename);
84                 foreach (var item in pss)
85                 {
86                     item .CloseMainWindow ();
87                     item .WaitForExit ();
88 
89                 }
90             i++;
91             Refresh ();
92 
93         }
94     }
95 }

 

posted @ 2016-06-26 21:13  蜀云泉  阅读(204)  评论(0)    收藏  举报