需要添加對System.Management.dll的引用 ,並且不要忘記導入下面的名稱空間。
using System.Management;
將ListView和 ImageList控件從可視工具箱拖動到Winform。

將ListView控件的LargeImageList屬性設置為ImageList。
添加代碼以處理表單加載事件,如下所示
private void button1_Click(object sender, EventArgs e) { using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select ProcessId, Name, ExecutablePath from Win32_Process")) { using (var results = managementObjectSearcher.Get()) { var processes = results.Cast<ManagementObject>().Select(p => new { ProcessId = (UInt32)p["ProcessId"], Name = (string)p["Name"], ExecutablePath = (string)p["ExecutablePath"] }); listView1.BeginUpdate(); foreach (var pro in processes) { if (System.IO.File.Exists(pro.ExecutablePath)) { var icon = Icon.ExtractAssociatedIcon(pro.ExecutablePath); var key = pro.ProcessId.ToString(); this.imageList1.Images.Add(key, icon.ToBitmap()); this.listView1.Items.Add(pro.Name, key); } } listView1.EndUpdate(); } } }
