簡體   English   中英

C#:如何獲取某個文件的圖標?

[英]C#: How can I get the icon for a certain file?

如果我有一個文件的路徑,有沒有辦法在Windows資源管理器中獲取Windows將為該文件顯示的圖標?

首先,有多種尺寸的圖像,其中一些比其他圖像更容易獲得。 如果你想要“普通”大小(我相信32x32)並且可以使用WPF(引用PresentationCore),這是一個很好的方法:

public System.Windows.Media.ImageSource Icon
{
    get
    {
        if (icon == null && System.IO.File.Exists(Command))
        {
            using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(Command))
            {
                // This new call in WPF finally allows us to read/display 32bit Windows file icons!
                icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                  sysicon.Handle,
                  System.Windows.Int32Rect.Empty,
                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
        }

        return icon;
    }
}
System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM