封装:Windows系统文件图标

原文:封装:Windows系统文件图标

用途:用于获取文件系统默认图标

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace HebianGu.ComLibModule.API
  9. {
  10. /// <summary> 获取文件关联图标 </summary>
  11. public partial class IconHelper
  12. {
  13. /// <summary> 返回系统设置的图标 </summary>
  14. /// <param name="pszPath">文件路径 如果为"" 返回文件夹的</param>
  15. /// <param name="dwFileAttributes">0</param>
  16. /// <param name="psfi">结构体</param>
  17. /// <param name="cbSizeFileInfo">结构体大小</param>
  18. /// <param name="uFlags">枚举类型</param>
  19. /// <returns>-1失败</returns>
  20. [DllImport("shell32.dll")]
  21. public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
  22. /// <summary> 获取文件图标 </summary>
  23. public Icon GetFileIcon(string p_Path)
  24. {
  25. SHFILEINFO _SHFILEINFO = new SHFILEINFO();
  26. IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));
  27. if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
  28. Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
  29. return _Icon;
  30. }
  31. /// <summary> 获取文件夹图标 </summary>
  32. public Icon GetDirectoryIcon()
  33. {
  34. SHFILEINFO _SHFILEINFO = new SHFILEINFO();
  35. IntPtr _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON));
  36. if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
  37. Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
  38. return _Icon;
  39. }
  40. /// <summary> 获取系统图标 </summary>
  41. public Icon GetSystemInfoIcon(string p_Path)
  42. {
  43. if (Path.HasExtension(p_Path))
  44. {
  45. try
  46. {
  47. return Icon.ExtractAssociatedIcon(p_Path);
  48. }
  49. catch
  50. {
  51. return null;
  52. }
  53. }
  54. else
  55. {
  56. return this.GetDirectoryIcon();
  57. }
  58. }
  59. }
  60. partial class IconHelper
  61. {
  62. #region - Start 单例模式 -
  63. /// <summary> 单例模式 </summary>
  64. private static IconHelper t = null;
  65. /// <summary> 多线程锁 </summary>
  66. private static object localLock = new object();
  67. /// <summary> 创建指定对象的单例实例 </summary>
  68. public static IconHelper Instance
  69. {
  70. get
  71. {
  72. if (t == null)
  73. {
  74. lock (localLock)
  75. {
  76. if (t == null)
  77. return t = new IconHelper();
  78. }
  79. }
  80. return t;
  81. }
  82. }
  83. /// <summary> 禁止外部实例 </summary>
  84. private IconHelper()
  85. {
  86. }
  87. #endregion - 单例模式 End -
  88. }
  89. [StructLayout(LayoutKind.Sequential)]
  90. public struct SHFILEINFO
  91. {
  92. public IntPtr hIcon;
  93. public IntPtr iIcon;
  94. public uint dwAttributes;
  95. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  96. public string szDisplayName;
  97. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  98. public string szTypeName;
  99. }
  100. public enum SHGFI
  101. {
  102. SHGFI_ICON = 0x100,
  103. SHGFI_LARGEICON = 0x0,
  104. SHGFI_USEFILEATTRIBUTES = 0x10
  105. }
  106. }

应用示例:

  1. /// <summary> 图片路径 </summary>
  2. public Icon ImagePath
  3. {
  4. get { return IconHelper.Instance.GetSystemInfoIcon(FilePath); }
  5. }


封装:Windows系统文件图标

上一篇:封装:WPF中可以绑定的BindPassWord控件


下一篇:abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之五(三十一)