问题
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