Icon of Installed Windows Applications
First we need to figure out the location of icon for the installed windows application. This information is stored in registry at following locations -
1. Key name - HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Value - DisplayIcon
2. Key name - HKEY_CLASSES_ROOT\Installer\Products\{productID}
Value - ProductIcon
first try to get location of icon from HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall. Here if the value of DisplayIcon is null then try to fetch location of icon from HKEY_CLASSES_ROOT\Installer\Products\{productID}
Here is the sample code to read the icon -
First we need to figure out the location of icon for the installed windows application. This information is stored in registry at following locations -
1. Key name - HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Value - DisplayIcon
2. Key name - HKEY_CLASSES_ROOT\Installer\Products\{productID}
Value - ProductIcon
first try to get location of icon from HEKY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall. Here if the value of DisplayIcon is null then try to fetch location of icon from HKEY_CLASSES_ROOT\Installer\Products\{productID}
Here is the sample code to read the icon -
using system.Drawing
//read HKEY_LOCAL_MACHINE
Icon ProductIcon;
private void ReadIcon()
{
try
{
string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
{
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
if (sk.GetValue("DisplayName") != null)
{
if (sk.GetValue("DisplayIcon") != null)
{
ProductIcon = Icon.ExtractAssociatedIcon(sk.GetValue("DisplayIcon").ToString());
}
else
{
//get icon from HKEY_CLASSES_ROOT
ProductIcon = GetIconForRoot(sk.GetValue("DisplayName").ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private Icon GetIconForRoot(string productName)
{
string producticon = "";
string InstallerKey = @"Installer\Products";
using (RegistryKey installkeys = Registry.ClassesRoot.OpenSubKey(InstallerKey))
{
foreach (string name in installkeys.GetSubKeyNames())
{
using (RegistryKey product = installkeys.OpenSubKey(name))
{
if (product.GetValue("ProductName") != null)
{
if (productName == product.GetValue("ProductName").ToString())
{
if (product.GetValue("ProductIcon") != null)
{
producticon = product.GetValue("ProductIcon").ToString();
}
}
}
}
}
}
if (Icon.ExtractAssociatedIcon(producticon) != null)
return Icon.ExtractAssociatedIcon(producticon);
else
return null;
}
In vb please
ReplyDeleteIn vb please
ReplyDelete