您可以使用ExtractIconEx() 从regedit.exe 中提取这些图标。
请注意,regedit.exe 可以在 Windows 和
Windows\System32 目录。在这里,我假设它在Windows 之下。
Win API 声明:
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, [Out] IntPtr[] phiconLarge, [Out] IntPtr[] phiconSmall, [In] uint nIcons);
[DllImport("user32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);
ExtractIconEx()首先用空参数调用,以获取文件包含的图标数量。
使用此信息,我们检索 2 个数组中的小图标和大图标句柄,并使用 Icon.FromHandle() 从这些句柄创建一个 GDI+ 图标。
string icnSource = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "regedit.exe");
uint icnNumber = ExtractIconEx(icnSource, -1, null, null, 1);
if (icnNumber > 0)
{
IntPtr[] phiconLarge = new IntPtr[icnNumber];
IntPtr[] phiconSmall = new IntPtr[icnNumber];
ExtractIconEx(icnSource, 0, phiconLarge, phiconSmall, icnNumber);
Icon[] iconsmall = new Icon[icnNumber];
Icon[] iconlarge = new Icon[icnNumber];
for (int x = 0; x < icnNumber; x++)
{
if (phiconLarge != null)
{
iconlarge[x] = (Icon)Icon.FromHandle(phiconLarge[x]).Clone();
DestroyIcon(phiconLarge[x]);
}
if (phiconSmall != null)
{
iconsmall[x] = (Icon)Icon.FromHandle(phiconSmall[x]).Clone();
DestroyIcon(phiconSmall[x]);
}
}
}
如有必要,您可以使用Icon.ToBitmap() 将图标转换为 GDI+ 位图。
这些,数字 3 和 4,是您正在寻找的图标:
pictureBox1.Image = iconsmall[3].ToBitmap();
pictureBox2.Image = iconsmall[4].ToBitmap();