编辑
2025-10-13
C#
00

目录

准备工作
方法一:使用 SHGetFileInfo 获取图标
图标提取方法
总结

在 Windows 系统开发中,有时我们需要获取文件、文件夹或特定文件类型的系统图标。本文将详细介绍几种在 C# 中提取系统图标的方法。

准备工作

在开始之前,需要引入以下命名空间:

C#
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms;

方法一:使用 SHGetFileInfo 获取图标

图标提取方法

C#
using System.Runtime.InteropServices; namespace AppGetIcon { public partial class Form1 : Form { private const uint SHGFI_ICON = 0x000000100; private const uint SHGFI_SMALLICON = 0x000000000; private const uint SHGFI_LARGEICON = 0x000000000; [DllImport("shell32.dll", CharSet = CharSet.Auto)] static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags ); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; } public Form1() { InitializeComponent(); } public Icon GetFileIcon(string filePath, bool isSmallIcon = true) { SHFILEINFO shinfo = new SHFILEINFO(); uint flags = SHGFI_ICON | (isSmallIcon ? SHGFI_SMALLICON : SHGFI_LARGEICON); IntPtr hImgSmall = SHGetFileInfo( filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags ); if (shinfo.hIcon == IntPtr.Zero) return null; return Icon.FromHandle(shinfo.hIcon); } private void btnGetIcon_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { string filePath = dialog.FileName; Icon icon = GetFileIcon(filePath); pictureBox1.Image = icon.ToBitmap(); } } } }

image.png

命名空间和引用:

C#
using System.Runtime.InteropServices;

这个引用用于支持与Windows API的互操作性。

常量定义:

C#
private const uint SHGFI_ICON = 0x000000100; // 获取图标 private const uint SHGFI_SMALLICON = 0x000000000; // 小图标 private const uint SHGFI_LARGEICON = 0x000000000; // 大图标

Windows API 导入:

C#
[DllImport("shell32.dll", CharSet = CharSet.Auto)] static extern IntPtr SHGetFileInfo(...)

这是导入Windows shell32.dll中的SHGetFileInfo函数,用于获取文件信息包括图标。

SHFILEINFO 结构:

C#
struct SHFILEINFO { public IntPtr hIcon; // 图标句柄 public int iIcon; // 图标索引 public uint dwAttributes; // 文件属性 public string szDisplayName; // 显示名称 public string szTypeName; // 类型名称 }

这个结构用于存储从Windows API获取的文件信息。

GetFileIcon 方法:

C#
public Icon GetFileIcon(string filePath, bool isSmallIcon = true)

这个方法接收文件路径和图标大小选项,返回文件的系统图标。主要步骤包括创建SHFILEINFO结构实例,设置适当的标志(小图标或大图标),调用SHGetFileInfo获取图标句柄,最后将句柄转换为Icon对象。

按钮点击事件处理:

C#
private void btnGetIcon_Click(object sender, EventArgs e)

当用户点击按钮时,会打开文件选择对话框,获取选中文件的路径,调用GetFileIcon获取图标,并将图标显示在PictureBox控件中。

功能流程:

用户点击按钮后打开文件选择对话框,选择文件后获取其图标,然后在界面上显示图标。

这个程序的主要用途是允许用户选择一个文件,然后显示该文件类型对应的系统图标。这对于创建文件浏览器或需要显示文件图标的应用程序很有用。

需要注意的是,代码使用了Windows API,因此这个程序只能在Windows系统上运行。另外,使用完Icon对象后应该适当释放资源以避免内存泄漏。

总结

通过 SHGetFileInfo 方法,我们可以轻松获取系统图标,无论是特定文件还是文件类型。合理使用这些方法,可以极大地增强应用程序的用户体验。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!