using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Management;
namespace A._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int fileIndex = 1;
string filename = "Notepad";
List<Data> list=new List<Data>();
public MainWindow()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
string arguement = Environment.CurrentDirectory + "myfile" + (fileIndex) + ".txt";
if (File.Exists(arguement) == false)
{
File.CreateText(arguement);
}
Process p = new Process();
p.StartInfo.FileName = filename;
p.StartInfo.Arguments = arguement;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.WaitForInputIdle();
RefreshProcessInfo();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
this.Cursor=Cursors.Wait;
Process [] myprocess;
myprocess =Process.GetProcessesByName(filename);
foreach( Process p in myprocess){
using (p){
p.CloseMainWindow();
Thread.Sleep(1000);
p.WaitForExit();
}
}
fileIndex=0;
RefreshProcessInfo();
this.Cursor=Cursors.Arrow;
}
private void RefreshProcessInfo()
{
this.dataGrid1.ItemsSource=null;
list.Clear();
Process[] processes = Process.GetProcesses();
foreach(Process p in processes){
Data pd = new Data();
try
{
if (p.Id == 0)
{
pd.ID = p.Id;
pd.Name = p.ProcessName;
pd.StartTime = "";
pd.Memory = "";
pd.FilePath = "";
}
else
{
pd.ID = p.Id;
pd.Name = p.ProcessName;
pd.StartTime = p.StartTime.ToString("yyyy-M-d HH:mm:ss");
pd.Memory = string.Format("{0,10:0} KB", p.WorkingSet64 / 1024d);
pd.FilePath = p.MainModule.FileName;
}
}
catch
{
pd.FilePath = "";
}
list.Add(pd);
}
this.dataGrid1.ItemsSource=list;
this.textblock.Text = "当前正在运行的进程数目是:" + list.Count;
}
}
public class Data{
public int ID {get;set;}
public string Name { get; set; }
public string StartTime { get; set; }
public string Memory { get; set; }
public string FilePath { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Management;
namespace A._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int fileIndex = 1;
string filename = "Notepad";
List<Data> list=new List<Data>();
public MainWindow()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
string arguement = Environment.CurrentDirectory + "myfile" + (fileIndex) + ".txt";
if (File.Exists(arguement) == false)
{
File.CreateText(arguement);
}
Process p = new Process();
p.StartInfo.FileName = filename;
p.StartInfo.Arguments = arguement;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.WaitForInputIdle();
RefreshProcessInfo();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
this.Cursor=Cursors.Wait;
Process [] myprocess;
myprocess =Process.GetProcessesByName(filename);
foreach( Process p in myprocess){
using (p){
p.CloseMainWindow();
Thread.Sleep(1000);
p.WaitForExit();
}
}
fileIndex=0;
RefreshProcessInfo();
this.Cursor=Cursors.Arrow;
}
private void RefreshProcessInfo()
{
this.dataGrid1.ItemsSource=null;
list.Clear();
Process[] processes = Process.GetProcesses();
foreach(Process p in processes){
Data pd = new Data();
try
{
if (p.Id == 0)
{
pd.ID = p.Id;
pd.Name = p.ProcessName;
pd.StartTime = "";
pd.Memory = "";
pd.FilePath = "";
}
else
{
pd.ID = p.Id;
pd.Name = p.ProcessName;
pd.StartTime = p.StartTime.ToString("yyyy-M-d HH:mm:ss");
pd.Memory = string.Format("{0,10:0} KB", p.WorkingSet64 / 1024d);
pd.FilePath = p.MainModule.FileName;
}
}
catch
{
pd.FilePath = "";
}
list.Add(pd);
}
this.dataGrid1.ItemsSource=list;
this.textblock.Text = "当前正在运行的进程数目是:" + list.Count;
}
}
public class Data{
public int ID {get;set;}
public string Name { get; set; }
public string StartTime { get; set; }
public string Memory { get; set; }
public string FilePath { get; set; }
}
}