代码之家  ›  专栏  ›  技术社区  ›  Svish

C:如何获取特定文件的图标?

  •  3
  • Svish  · 技术社区  · 15 年前

    如果我有一个文件的路径,有没有一种方法可以让Windows在Windows资源管理器中显示该文件的图标?

    2 回复  |  直到 12 年前
        1
  •  6
  •   Sam Harwell    15 年前

    首先,有多种尺寸的图像,其中一些比其他更容易获得。如果您想要“标准”尺寸的(我相信是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;
        }
    }
    
        2
  •  0
  •   Keith Maurino    14 年前
    System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath);