根据文件名或文件扩展名获取文件的默认图标
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace findIconWithPathFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string xx = null;
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(xx) == true)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.CheckFileExists = true;
dlg.CheckPathExists = true;
dlg.Filter = "所有文件(*.*)|*.*";
if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
xx = dlg.FileName;
}
}
//Icon icon= getIcon(xx ,true);
Icon icon = getIcon1(xx);
pictureBox1.Image = icon.ToBitmap();
}
}
[DllImport("shell32.dll")]
public extern static uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
public Icon getIcon(string pathxx, bool large)
{
int[] phiconLarge = new int[1];
int[] phiconSmall = new int[1];
ExtractIconEx(pathxx, 0, phiconLarge, phiconSmall, 1);
IntPtr handle = new IntPtr(large ? phiconLarge[0] : phiconSmall[0]);
Icon icon = Icon.FromHandle(handle);
return icon;
}
public Icon getIcon1(string pathxx)
{
Icon icon = Icon.ExtractAssociatedIcon(pathxx);
return icon;
}
}
}
/// 依据文件名读取图标,若指定文件不存在,则返回空值。
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static Icon GetIconByFileName(string fileName, bool isLarge = true)
{
int[] phiconLarge = new int[1];
int[] phiconSmall = new int[1];
//文件名 图标索引
try
{
Win32.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1);
IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
return Icon.FromHandle(IconHnd);
}
catch {
return null;
}
}
/// 根据文件扩展名(如:.*),返回与之关联的图标。
/// 若不以"."开头则返回文件夹的图标。
/// </summary>
/// <param name="fileType">文件扩展名</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static Icon GetIconByFileType(string fileType, bool isLarge)
{
if (fileType == null || fileType.Equals(string.Empty)) return null;
RegistryKey regVersion = null;
string regFileType = null;
string regIconString = null;
string systemDirectory = Environment.SystemDirectory + "\\";
if (fileType[0] == '.')
{
//读系统注册表中文件类型信息
regVersion = Registry.ClassesRoot.OpenSubKey(fileType, true);
if (regVersion != null)
{
regFileType = regVersion.GetValue("") as string;
regVersion.Close();
regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", true);
if (regVersion != null)
{
regIconString = regVersion.GetValue("") as string;
regVersion.Close();
}
}
if (regIconString == null)
{
//没有读取到文件类型注册信息,指定为未知文件类型的图标
regIconString = systemDirectory + "shell32.dll,0";
}
}
else
{
//直接指定为文件夹图标
regIconString = systemDirectory + "shell32.dll,3";
}
string[] fileIcon = regIconString.Split(new char[] { ',' });
if (fileIcon.Length != 2)
{
//系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标
fileIcon = new string[] { systemDirectory + "shell32.dll", "2" };
}
Icon resultIcon = null;
try
{
//调用API方法读取图标
int[] phiconLarge = new int[1];
int[] phiconSmall = new int[1];
uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);
IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
resultIcon = Icon.FromHandle(IconHnd);
}
catch { }
return resultIcon;
}
}
using System.Runtime.InteropServices;
using System.Drawing;
namespace MyNamespace
{
public class FileIcon
{
/// <summary>
/// 获取文件的默认图标
/// </summary>
/// <param name="fileName">文件名。
/// 可以只是文件名,甚至只是文件的扩展名(.*);
/// 如果想获得.ICO文件所表示的图标,则必须是文件的完整路径。
/// </param>
/// <param name="largeIcon">是否大图标</param>
/// <returns>文件的默认图标</returns>
public static Icon GetFileIcon(string fileName, bool largeIcon)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
SHGetFileInfo(fileName, 256, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
}
}
相关文章
- Linux文件共享(二)——两个独立进程打开同一个文件
- [C++]文件头引入#ifndef与#define有什么作用
- 【C/C++学院】(18)QT文件读写/主窗口类/获取host信息
- python应用-获取文件后缀名
- 【Java】如何获取文件的创建时间、更新时间
- 浏览器前端js获取文件的MD5值
- java.util.Arrays.sort两种方式的排序(及文件读写练习)
- JS魔法堂:获取当前脚本文件的绝对路径
- loadrunner11 执行bat批处理文件时获取bat文件所在路径 正确方式与采坑说明
- linux下查找最近修改过的文件.
- QT获取文件信息
- 根据第三方提供的webservice地址获取文件信息
- PCL 从LAS文件中获取点云的坐标
- scala maven完整pom文件:编译、打包插件配置等
- 配置和修改springboot默认国际化文件
- Python语言学习:创建/删除文件/文件夹、获取当前文件/文件夹路径(系统环境路径/目录)、获取当前文件夹下的所有子文件路径等代码(os系列用法)实现之详细攻略
- 你知道,java项目中是如何获取文件地址的吗?
- 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】
- Java中的URL类&根据url获取网络文件&快速入门Java中的URL(网络编程)
- 【java项目实战】dom4j解析xml文件,连接Oracle数据库
- linux - 解压tgz 文件指令
- Http服务器实现文件上传与下载(五)
- Python编程:pkgutil获取包中的资源文件
- 【Android 安全】DEX 加密 ( 代理 Application 开发 | multiple-dex-core 依赖库开发 | 配置元数据 | 获取 apk 文件并准备相关目录 )
- Java反射机制获取Class文件
- 如何js编译的文件dll对于网页电话
- Linux 该文件命令查看内容
- Springboot+WebUploader优雅实现超大文件的上传(二)
- Qt——实现一个简单的获取文件信息的dialog