C#网络编程之进程管理

  这里是视频讲解地址:

  这里是代码:

  

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Net;
  5 using System.IO;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows;
  9 using System.Windows.Controls;
 10 using System.Windows.Data;
 11 using System.Windows.Documents;
 12 using System.Windows.Input;
 13 using System.Windows.Media;
 14 using System.Windows.Media.Imaging;
 15 using System.Windows.Navigation;
 16 using System.Windows.Shapes;
 17 using System.Diagnostics;
 18 
 19 namespace 我是蜀云泉
 20 {
 21     /// <summary>
 22     /// MainWindow.xaml 的交互逻辑
 23     /// </summary>
 24     public partial class MainWindow : Window
 25     {
 26         public MainWindow()
 27         {
 28             InitializeComponent();
 29         }
 30 
 31         int FileIndex = 1;
 32         string FileName = "Notepad";
 33         List<Data> list = new List<Data>();
 34         List<int> IndexList = new List<int>();
 35 
 36         private void Button_Click_1(object sender, RoutedEventArgs e)
 37         {
 38             string Path = Environment.CurrentDirectory + "//myfile" + (FileIndex++) + ".txt";
 39             if (File.Exists(Path)==false)
 40             {
 41                 File.CreateText(Path);
 42             }
 43             IndexList.Add(FileIndex);
 44 
 45             Process p = new Process();
 46             p.StartInfo.FileName = FileName;
 47             p.StartInfo.Arguments = Path;
 48             p.Start();
 49 
 50             Refresh();
 51         }
 52 
 53 
 54         public void Refresh()
 55         {
 56             datagrid1.ItemsSource = null;
 57             list.Clear();
 58             Process[] processes = Process.GetProcessesByName(FileName);
 59             foreach (var item in processes)
 60             {
 61                 try
 62                 {
 63                     list.Add(new Data (){ 
 64                     Id=item.Id,
 65                     ProcessName=item.ProcessName,
 66                     Memory=string.Format("{0}KB",item.WorkingSet64/1024d),
 67                     Time = item.StartTime.ToString("yyyy-M-d HH:mm:ss"),
 68                     FileNamePath = item.MainModule.FileName
 69                     });
 70                     
 71                 }
 72                 catch 
 73                 {
 74                     break;
 75                 }
 76             }
 77             datagrid1.ItemsSource = list;
 78 
 79         }
 80 
 81 
 82         private void Button_Click_2(object sender, RoutedEventArgs e)
 83         {
 84             Process[] processes = Process.GetProcessesByName(FileName);
 85             foreach (var item in processes)
 86             {
 87                 item.CloseMainWindow();
 88                 item.WaitForExit();
 89             }
 90             FileIndex = 1;
 91 
 92             Refresh();
 93             
 94         } 
 95 
 96         public  class Data
 97         {
 98             public int Id{get;set;}
 99             public string ProcessName { get; set; }
100             public string Memory { get; set; }
101             public string Time { get; set; }
102             public string FileNamePath { get; set; }
103         }
104 
105        
106         
107     }
108 }
原文地址:https://www.cnblogs.com/yunquan/p/5616767.html