Getting programs icons in C#?

孤街浪徒 提交于 2019-12-23 21:33:28

问题


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);
            }
        }

回答1:


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.
}



回答2:


Try this:

Icon  result = Icon.ExtractAssociatedIcon(filePath); 


来源:https://stackoverflow.com/questions/11323508/getting-programs-icons-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!