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

C# 从进程列表而不是文件中获取图标,c#,.net,winforms,C#,.net,Winforms,如何从processlist中提取图标,而不是当前的文件名?到目前为止,这项工作是通过打开表单对话框,单击一个文件,然后用图标将其添加到listView中。如何获取进程图标并在listView中显示它们 [StructLayout(LayoutKind.Sequential)] public struct SHFILEINFO { public IntPtr hIcon; public IntPtr iIcon; public uint dwAttributes;

如何从processlist中提取图标,而不是当前的文件名?到目前为止,这项工作是通过打开表单对话框,单击一个文件,然后用图标将其添加到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++);
    }
请看这里:

您可以将其替换为现有代码。或者,根据你现在正在做的事情进行调整

编辑:添加代码以获取进程

请参见此处:

您可以将其替换为现有代码。或者,根据你现在正在做的事情进行调整


编辑:添加了获取进程的代码

您可以使用上的WMI查询查找进程信息,并使用ExecutablePath查找进程的可执行路径。然后,您可以使用提取流程的关联图标:

范例

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

添加对System.Management.dll的引用,并添加使用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);
        }
    }
}
那么你就会得到这样的结果:


您可以使用上的WMI查询查找进程信息,并使用ExecutablePath查找进程的可执行路径。然后,您可以使用提取流程的关联图标:

范例

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

添加对System.Management.dll的引用,并添加使用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);
        }
    }
}
那么你就会得到这样的结果:

32位和64位进程的解决方案 只有系统诊断

这将从exe文件中获取主模块的图标。如果访问和目标流程的架构不同,它也会起作用:

var icon = Process.GetProcessById(1234).GetIcon()
使用扩展方法:

public static class ProcessExtensions {
    [DllImport("Kernel32.dll")]
    private static extern uint QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize);

    public static string GetMainModuleFileName(this Process process, int buffer = 1024) {
        var fileNameBuilder = new StringBuilder(buffer);
        uint bufferLength = (uint)fileNameBuilder.Capacity + 1;
        return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) != 0 ?
            fileNameBuilder.ToString() :
            null;
    }

    public static Icon GetIcon(this Process process) {
        try {
            string mainModuleFileName = process.GetMainModuleFileName();
            return Icon.ExtractAssociatedIcon(mainModuleFileName);
        }
        catch {
            // Probably no access
            return null;
        }
    }
}
32位和64位进程的解决方案 只有系统诊断

这将从exe文件中获取主模块的图标。如果访问和目标流程的架构不同,它也会起作用:

var icon = Process.GetProcessById(1234).GetIcon()
使用扩展方法:

public static class ProcessExtensions {
    [DllImport("Kernel32.dll")]
    private static extern uint QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize);

    public static string GetMainModuleFileName(this Process process, int buffer = 1024) {
        var fileNameBuilder = new StringBuilder(buffer);
        uint bufferLength = (uint)fileNameBuilder.Capacity + 1;
        return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) != 0 ?
            fileNameBuilder.ToString() :
            null;
    }

    public static Icon GetIcon(this Process process) {
        try {
            string mainModuleFileName = process.GetMainModuleFileName();
            return Icon.ExtractAssociatedIcon(mainModuleFileName);
        }
        catch {
            // Probably no access
            return null;
        }
    }
}

这个问题和你的有什么区别?@RezaAghaei好吧,现在这个问题适用于任何一位。这个问题和你的问题有什么区别?@RezaAghaei好吧,现在这个问题适用于任何一位。目标是获得所有进程的图标。不是得到进程。对,你得到了进程,然后你使用你已经拥有的代码来完成剩下的工作。您没有在现有代码中使用文件名,而是将此代码替换到原来的位置。访问被拒绝。仅适用于32位。任何解决方案,如果它不仅在32位上工作?对于没有ManagementObjectSearcher的解决方案,但同时在32位和64位上工作,请查看。目标是获得所有进程的图标。不是得到进程。对,你得到了进程,然后你使用你已经拥有的代码来完成剩下的工作。您没有在现有代码中使用文件名,而是将此代码替换到原来的位置。访问被拒绝。仅适用于32位。任何解决方案,如果它不仅在32位上工作?对于不带ManagementObjectSearcher的解决方案,但同时适用于32位和64位,请查看。