获取程序的图标在C#中? [英] Getting programs icons in C#?
问题描述
我有这样的代码,将抢到的名字,但我如何得到每个程序的图标?
字符串SoftwareKey =SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\ \\\UserData\\S-1-5-18\\Products
的RegistryKey RK =默认(的RegistryKey);
RK = Registry.LocalMachine.OpenSubKey(SoftwareKey);
字符串SNAME =的String.Empty;
的foreach(在rk.GetSubKeyNames串skname())
{
试
{
SNAME = Registry.LocalMachine.OpenSubKey( 。SoftwareKey).OpenSubKey(skname).OpenSubKey(InstallProperties)的GetValue(显示名称)的ToString(); 。
串INST1 = Registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey(InstallProperties)的GetValue(INSTALLLOCATION)的ToString();
INT N = dataGridView1.Rows.Add();
dataGridView1.Rows [N] .Cells [2]。价值= SNAME;
dataGridView1.Rows [N] .Cells [3] .value的= INST1;
}
赶上(异常前)
{
//MessageBox.Show(ex.Message);
}
}
我'M不知道 InstallProperties
会给你安装的可执行文件(如确安装程序可以安装多个可执行文件)。
如果你要确定正确的可执行文件的手段(包括可能列举的.exe文件 INSTALLLOCATION
),您可以然后抓住从.exe文件的默认图标。
有关详细信息,请参阅
的获取壳牌使用的文件图标
更新
下面的代码的未经检验,但应该让你八九不离十:
<$ 。C $ C>字符串INST1 = registry.LocalMachine.OpenSubKey(SoftwareKey).OpenSubKey(skname).OpenSubKey(InstallProperties)的GetValue(INSTALLLOCATION)的ToString();
的foreach(在Directory.GetFiles(字符串文件INST1,一个* .exe))
{
字符串文件路径= Path.Combine(INST1,文件);
图标结果= Icon.ExtractAssociatedIcon(文件路径);
//如果结果不为空,你有一个图标。
}
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);
}
}
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
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.
}
这篇关于获取程序的图标在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!