C# 读取一个快捷方式的Icon图标

安装在Windows上的应用发送到桌面快捷方式时,通常会带上一个Icon图标,这个图标地址不固定,本文将尝试如何获得该Icon:

/// <summary>
/// 读取一个快捷方式的Icon
/// </summary>
private void ReadShortcut(string 快捷方式路径, out string iconPath)
{
    try
    {
        using ShellObject shellObject = ShellObject.FromParsingName(快捷方式路径);
        ShellThumbnail shellThumbnail = shellObject.Thumbnail;
        shellThumbnail.FormatOption = ShellThumbnailFormatOption.Default;
        var bitmap = shellThumbnail.Bitmap;
        bitmap.MakeTransparent();
        using FileStream stream = File.Create(要保存图片的地址);
        //这里将bitmap保存,如有需要可以修改返回类型直接返回bitmap
        bitmap.Save(stream, ImageFormat.Png);
        iconPath = filePath;
    }
    catch (Exception ex)
    {
        iconPath = string.Empty;
        Log.Error($"解析Icon时出错:{ex}");
    }
}
posted @ 2026-08-10 10:56  Poetindusk  阅读(1)  评论(0)    收藏  举报