using System; using System.Windows; using System.Diagnostics; namespace GetProcessForm { /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click_1(object sender, RoutedEventArgs e) { Process[] all = Process.GetProcesses(); int length = all.Length; for (int index = 0; index < length; index++) { Console.WriteLine(String.Format("{0} ID:{1}", all[index].ProcessName, all[index].Id)); } } private void Button_Click_2(object sender, RoutedEventArgs e) { Process []ps = Process.GetProcessesByName("chrome"); foreach (Process p in ps) { Console.WriteLine(String.Format("{0} ID:{1}", p.ProcessName, p.Id)); } } } }
Button1執行結果:
Button2執行結果:
參考資料:
http://msdn.microsoft.com/zh-tw/library/System.Diagnostics.Process_methods(v=vs.110).aspx