3

I have this code that will grab the names, but how do i get each program's icon?

 string SoftwareKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products";
        RegistryKey rk = default(RegistryKey);
        rk = Registry.LocalMachine.OpenSubKey(SoftwareKey);

        string sname = string.Empty;

        foreach (string skname in rk.GetSubKeyNames())
        {

            try
            {
                sname = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey("InstallProperties").GetValue("DisplayName").ToString();
                string Inst1 = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey("InstallProperties").GetValue("InstallLocation").ToString();
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[2].Value = sname; 
                dataGridView1.Rows[n].Cells[3].Value = Inst1;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116

2 Answers2

4

I'm not aware that InstallProperties will give you the installed executable (as indeed an installer could install multiple executable files).

If you have a means to determine the correct executable (including perhaps enumerating the .exe files in InstallLocation), you could then grab the default icon from that .exe.

For details, see

Get File Icon used by Shell

UPDATE

The following code is untested but should get you pretty close:

string Inst1 = registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey("InstallProperties").GetValue("InstallLocation").ToString();

foreach (string file in Directory.GetFiles(Inst1, "*.exe")) 
{
    string filePath = Path.Combine(Inst1, file);
    Icon  result = Icon.ExtractAssociatedIcon(filePath);
    // If result is not null, you have an icon.
}
Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • i am using C# and...how do i do that? – Hunter Mitchell Jul 04 '12 at 06:38
  • Added code to show how to search a given folder for .exe files and pull the icon. Don't recall off the top of my head whether `Directory.GetFiles()` returns just the file name or full path to the file, so you may not need the `Path.Combine` line. Thanks to @ebad86 for the concise method to get the icon. – Eric J. Jul 04 '12 at 06:45
2

Try this:

Icon  result = Icon.ExtractAssociatedIcon(filePath); 
Ebad Masood
  • 2,389
  • 28
  • 46