c# - 从进程列表而不是文件中获取图标

标签 c# .net winforms

目前我如何能够从进程列表中提取图标而不是文件名?到目前为止,这是通过打开表单对话框来工作的,他们单击一个文件,然后将其添加到带有图标的 listView 中。如何才能仅获取进程图标并将其显示在 ListView 中?

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
    public IntPtr hIcon;
    public IntPtr iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string szTypeName;
};

class Win32
{
    public const uint SHGFI_ICON = 0x100;
    public const uint SHGFI_LARGEICON = 0x0;    // 'Large icon
    public const uint SHGFI_SMALLICON = 0x1;    // 'Small icon

    [DllImport("shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath,
                                uint dwFileAttributes,
                                ref SHFILEINFO psfi,
                                uint cbSizeFileInfo,
                                uint uFlags);
}

private int nIndex = 0;
private void materialFlatButton13_Click_1(object sender, EventArgs e)
{
    IntPtr hImgSmall;    //the handle to the system image list
    IntPtr hImgLarge;    //the handle to the system image list
    string fName;        // 'the file name to get icon from
    SHFILEINFO shinfo = new SHFILEINFO();

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.InitialDirectory = "c:\\temp\\";
    openFileDialog1.Filter = "All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    listView1.SmallImageList = imageList1;
    listView1.LargeImageList = imageList1;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        fName = openFileDialog1.FileName;
        //Use this to get the small Icon
        hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,
                                       (uint)Marshal.SizeOf(shinfo),
                                        Win32.SHGFI_ICON |
                                        Win32.SHGFI_SMALLICON);

        System.Drawing.Icon myIcon =
               System.Drawing.Icon.FromHandle(shinfo.hIcon);

        imageList1.Images.Add(myIcon);

        //Add file name and icon to listview
        listView1.Items.Add(fName, nIndex++);
    }

最佳答案

您可以在 Win32_Process 上使用 WMI 查询查找进程信息并使用 ExecutablePath 查找进程的可执行路径。然后你可以使用Icon.ExtractAssociatedIcon提取进程的相关图标:

示例

在窗体上拖放一个 ImageList 并将其 ColorDepth 设置为 Depth32Bit 并将其 ImageSize 设置为 32,32。在表单上放置一个 ListView 并将其 LargImageList 设置为您在第一步中创建的 imageList1

添加对 System.Management.dll 的引用并添加 using System.Management; 并使用以下代码用图标填充 listView1:

var query = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process";
using (var searcher = new ManagementObjectSearcher(query))
using (var results = searcher.Get())
{
    var processes = results.Cast<ManagementObject>().Select(x => new
    {
        ProcessId = (UInt32)x["ProcessId"],
        Name = (string)x["Name"],
        ExecutablePath = (string)x["ExecutablePath"]
    });
    foreach (var p in processes)
    {
        if (System.IO.File.Exists(p.ExecutablePath))
        {
            var icon = Icon.ExtractAssociatedIcon(p.ExecutablePath);
            var key = p.ProcessId.ToString();
            this.imageList1.Images.Add(key, icon.ToBitmap());
            this.listView1.Items.Add(p.Name, key);
        }
    }
}

那么你会得到这样的结果:

enter image description here

关于c# - 从进程列表而不是文件中获取图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43011012/

相关文章:

c# 用对象区分两个列表

c# - 调用方法并直接读取/设置参数的私有(private)字段是否会破坏封装?

javascript - 将类库 (.net) 的 Javascript 文件链接到 blazor 应用程序

winforms - 如何使用 C# 将 Windows 窗体面板作为位图获取?

c# - 从列表中实例化的类中获取字段列表,并比较它们

c# - 如何从 Pocket(稍后阅读)或 Readability 等 HTML 页面中提取文章文本内容?

.net - 如果您使用 Azure AppService 和 SSL,是否有必要通过 SSL 运行 docker 容器?

c# - 从当前登录用户的 Active Directory 中获取专有名称

c# - 如何在没有重复记录的情况下使用 DataTable.Load(DataReader) 重新加载 DataGridView

c# - 保存设置时未保存 OrderedDictionary