获取带有应用程序图标的已安装程序列表

7

我需要获取本地计算机上已安装程序的列表,并显示应用程序图标。以下是我使用的代码片段,用于获取已安装程序的列表和安装目录路径。

/// <summary>
    /// Gets a list of installed software and, if known, the software's install path.
    /// </summary>
    /// <returns></returns>
    private string Getinstalledsoftware()
    {
        //Declare the string to hold the list:
        string Software = null;

        //The registry key:
        string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
        {
            //Let's go through the registry keys and get the info we need:
            foreach (string skName in rk.GetSubKeyNames())
            {
                using (RegistryKey sk = rk.OpenSubKey(skName))
                {
                    try
                    {
                        //If the key has value, continue, if not, skip it:
                        if (!(sk.GetValue("DisplayName") == null))
                        {
                            //Is the install location known?
                            if (sk.GetValue("InstallLocation") == null)
                                Software += sk.GetValue("DisplayName") + " - Install path not known\n"; //Nope, not here.
                            else
                                Software += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; //Yes, here it is...
                        }
                    }
                    catch (Exception ex)
                    {
                        //No, that exception is not getting away... :P
                    }
                }
            }
        }

        return Software;
    }

现在的问题是如何获取已安装应用程序的图标呢?
提前感谢。

以上代码还包括窗口更新,我该如何排除这些程序? - MUS
2个回答

8
要确定是否为更新,将会有一个名为IsMinorUpgrade的键。这个键存在且设置为1表示为更新,如果是0或者不存在,则不是更新。
要从可执行文件中获取图标,请使用以下代码: VB:
Public Function IconFromFilePath(filePath As String) As Icon 
    Dim result As Icon = Nothing 
    Try 
        result = Icon.ExtractAssociatedIcon(filePath) 
    Catch ''# swallow and return nothing. You could supply a default Icon here as well 
    End Try 
    Return result 
End Function 

C#:

public Icon IconFromFilePath(string filePath)
{
    Icon result = null;
    try {
        result = Icon.ExtractAssociatedIcon(filePath);
    } catch { }
    return result;
}

我正在使用“DisplayIcon”键获取已安装程序的图标,但与WIN XP控制面板中提供的添加/删除程序实用程序相比,结果不同。有什么建议吗? - MUS
这很奇怪。我没有任何想法,但我会看看能否更深入地研究一下。 - Icemanind

0

提取已安装 Windows 应用程序的图标,首先我们需要找出已安装应用程序的图标位置。此信息存储在注册表中以下位置 -

  1. 键名 - HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 值 - DisplayIcon
  2. 键名 - HKEY_CLASSES_ROOT\Installer\Products{productID} 值 - ProductIcon

要获取应用程序图标的详细信息和代码,请访问 - http://newapputil.blogspot.in/2015/06/extract-icons-of-installed-windows_17.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接