C#中如何获取文件图标
程序员文章站
2022-05-14 17:34:03
本文给大家介绍如何去获取一个文件的关联图标呢?现总结如下:
首先明确问题:获取一个文件的关联图标或者是某个类型文件的显示图标。
在网上搜了一...
本文给大家介绍如何去获取一个文件的关联图标呢?现总结如下:
首先明确问题:获取一个文件的关联图标或者是某个类型文件的显示图标。
在网上搜了一圈,发现方法还是比较多的,但是应用c#进行获取的方法不多。我选择一种用.net库的方法。
使用的类:
system.drawing.icon ,位于system.drawing 命名空间。
具体方法:
system.drawing.icon 类中的静态方法:public static icon extractassociatedicon(string filepath)
此方法接收一个文件路径,返回一个 icon 类型的对象。此对象既是指定文件的 icon 图标对象。
此 icon 类提供了 save 方法,可以将 icon 文件写入流中,进而保存至硬盘文件。
当然要在程序中显示也是可以的,icon 类提供了 tobitmap 方法,可以将 icon 文件转换为 bitmap。bitmap 就可以在 picturebox 中显示了。
如果要根据后缀名来获取文件图标,那要怎么办呢?我想了一个比较笨的办法。看代码~
string filename = "tmp." + houz*g; file.create(filename).close(); image img = system.drawing.icon.extractassociatedicon(filename).tobitmap(); file.delete(filename);
如此,新建一个指定后缀名的文件,获取图标后直接删除,就能获得指定文件后缀名的图标了。嘻嘻~
下面给大家分享一段代码介绍c#获取文件关联图标
using system; using system.collections.generic; using system.text; using system.runtime.interopservices; using system.drawing; using microsoft.win32; namespace ifmdp.devicemanager { class systemicon { [structlayout(layoutkind.sequential, charset = charset.auto)] public struct shfileinfo { public intptr hicon; public int iicon; public uint dwattributes; [marshalas(unmanagedtype.byvaltstr, sizeconst = 260)] public string szdisplayname; [marshalas(unmanagedtype.byvaltstr, sizeconst = 80)] public string sztypename; } [dllimport("shell32.dll", entrypoint = "shgetfileinfo", setlasterror = true, charset = charset.auto)] public static extern intptr shgetfileinfo(string pszpath, uint dwfileattributes, ref shfileinfo psfi, uint cbfileinfo, uint uflags); [dllimport("user32.dll", entrypoint = "destroyicon")] public static extern int destroyicon(intptr hicon); #region api 参数的常量定义 public enum fileinfoflags : uint { shgfi_icon = 0x000000100, // get icon shgfi_displayname = 0x000000200, // get display name shgfi_typename = 0x000000400, // get type name shgfi_attributes = 0x000000800, // get attributes shgfi_iconlocation = 0x000001000, // get icon location shgfi_exetype = 0x000002000, // return exe type shgfi_sysiconindex = 0x000004000, // get system icon index shgfi_linkoverlay = 0x000008000, // put a link overlay on icon shgfi_selected = 0x000010000, // show icon in selected state shgfi_attr_specified = 0x000020000, // get only specified attributes shgfi_largeicon = 0x000000000, // get large icon shgfi_smallicon = 0x000000001, // get small icon shgfi_openicon = 0x000000002, // get open icon shgfi_shelliconsize = 0x000000004, // get shell size icon shgfi_pidl = 0x000000008, // pszpath is a pidl shgfi_usefileattributes = 0x000000010, // use passed dwfileattribute shgfi_addoverlays = 0x000000020, // apply the appropriate overlays shgfi_overlayindex = 0x000000040 // get the index of the overlay } public enum fileattributeflags : uint { file_attribute_readonly = 0x00000001, file_attribute_hidden = 0x00000002, file_attribute_system = 0x00000004, file_attribute_directory = 0x00000010, file_attribute_archive = 0x00000020, file_attribute_device = 0x00000040, file_attribute_normal = 0x00000080, file_attribute_temporary = 0x00000100, file_attribute_sparse_file = 0x00000200, file_attribute_reparse_point = 0x00000400, file_attribute_compressed = 0x00000800, file_attribute_offline = 0x00001000, file_attribute_not_content_indexed = 0x00002000, file_attribute_encrypted = 0x00004000 } #endregion /// <summary> /// 获取文件类型的关联图标 /// </summary> /// <param name="filename">文件类型的扩展名或文件的绝对路径</param> /// <param name="islargeicon">是否返回大图标</param> /// <returns>获取到的图标</returns> public static icon geticon(string filename, bool islargeicon) { shfileinfo shfi = new shfileinfo(); intptr hi; if (islargeicon) hi = shgetfileinfo(filename, 0, ref shfi, (uint)marshal.sizeof(shfi), (uint)fileinfoflags.shgfi_icon | (uint)fileinfoflags.shgfi_usefileattributes | (uint)fileinfoflags.shgfi_largeicon); else hi = shgetfileinfo(filename, 0, ref shfi, (uint)marshal.sizeof(shfi), (uint)fileinfoflags.shgfi_icon | (uint)fileinfoflags.shgfi_usefileattributes | (uint)fileinfoflags.shgfi_smallicon); icon icon = icon.fromhandle(shfi.hicon).clone() as icon; destroyicon(shfi.hicon); //释放资源 return icon; } /// <summary> /// 获取文件夹图标 /// </summary> /// <returns>图标</returns> public static icon getdirectoryicon(bool islargeicon) { shfileinfo _shfileinfo = new shfileinfo(); intptr _iconintptr; if (islargeicon) { _iconintptr = shgetfileinfo(@"", 0, ref _shfileinfo, (uint)marshal.sizeof(_shfileinfo), ((uint)fileinfoflags.shgfi_icon | (uint)fileinfoflags.shgfi_largeicon)); } else { _iconintptr = shgetfileinfo(@"", 0, ref _shfileinfo, (uint)marshal.sizeof(_shfileinfo), ((uint)fileinfoflags.shgfi_icon | (uint)fileinfoflags.shgfi_smallicon)); } if (_iconintptr.equals(intptr.zero)) return null; icon _icon = system.drawing.icon.fromhandle(_shfileinfo.hicon); return _icon; } } }
上一篇: ORACLE 笔记
下一篇: MongoDB多条件模糊查询示例代码