C#提取程序的圖標


需要添加對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();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM