完成类似任务管理器界面
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
M1();
}
List<Data> list = new List<Data>();
private void b1_Click(object sender, RoutedEventArgs e)
{
string k = t3.Text;
Process p = new Process();
p.StartInfo.FileName = k;
try
{
p.Start();
}
catch (Exception)
{
MessageBox.Show("启动失败");
}
}
private void b2_Click(object sender, RoutedEventArgs e)
{
var a = t1.SelectedItem;
Data da = a as Data;
Process p = Process.GetProcessById(da.Id);
try
{
p.Kill();
}
catch (Exception)
{
MessageBox.Show("error");
}
M1();
}
private void M1() {
t1.ItemsSource = null;
list.Clear();
Process[] p = Process.GetProcesses();
tt.Text= "进程数:" p.Length.ToString();
foreach (Process item in p)
{
Data data = new Data();
data.Id = item.Id;
try
{
data.Name = item.ProcessName;
}
catch (Exception)
{
data.Name = "";
}
data.Memory=item.WorkingSet64.ToString();
try
{
data.dateTime =item.StartTime.ToString();
}
catch (Exception)
{
data.dateTime = "";
}
try
{
data.Filename=item.MainModule.FileName;
}
catch (Exception)
{
data.Filename = "";
}
list.Add(data);
}
t1.ItemsSource = list;
}
public class Data {
public int Id { get; set; }
public String Name { get; set; }
public String Memory { get; set; }
public String dateTime { get; set; }
public String Filename { get; set; }
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
评论