• C#获取文件图标


    [StructLayout(LayoutKind.Sequential)]
            public struct SHFILEINFO
            {
                public IntPtr hIcon;
                public IntPtr iIcon;
                public uint dwAttributes;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string szDisplayName;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string szTypeName;
            }
    
            /// <summary>  
            /// 返回系统设置的图标  
            /// </summary>  
            /// <param name="pszPath">文件路径 如果为""  返回文件夹的</param>  
            /// <param name="dwFileAttributes">0</param>  
            /// <param name="psfi">结构体</param>  
            /// <param name="cbSizeFileInfo">结构体大小</param>  
            /// <param name="uFlags">枚举类型</param>  
            /// <returns>-1失败</returns>  
            [DllImport("shell32.dll")]
            public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref   SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    
            public enum SHGFI
            {
                SHGFI_ICON = 0x100,
                SHGFI_LARGEICON = 0x0,
                SHGFI_USEFILEATTRIBUTES = 0x10
            }
    
    
            /// <summary>  
            /// 获取文件图标 
            /// </summary>  
            /// <param name="p_Path">文件全路径</param>  
            /// <returns>图标</returns>  
            public static Icon GetFileIcon(string p_Path)
            {
                Icon _Icon = Properties.Resources.fileIco;
                try
                {
                    SHFILEINFO _SHFILEINFO = new SHFILEINFO();
                    IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));
                    if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
                    _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
                }
                catch
                {
                }
                return _Icon;
            }
    //调用的时候这么调用
    Icon tempIcon = GetFileIcon(fileName);
  • 相关阅读:
    现在分词做状语,到,非谓语动词
    成功和失败因素收集
    退拽原理2
    分享到(事件冒泡实例)
    滚动公告(纵向)
    RabbitMQ消息队列(一): Detailed Introduction 详细介绍
    在IDEA中实战Git
    深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
    MySQL中日期和时间戳互相转换的函数和方法
    【Docker】 windows10 docker 使用
  • 原文地址:https://www.cnblogs.com/qq1223558/p/3118928.html
Copyright © 2020-2023  润新知