根据文件名或文件扩展名获取文件的默认图标
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
}
}
}
根据文件名或文件扩展名获取文件的默认图标相关推荐
- 从文件扩展名获取MIME类型
本文翻译自:Get MIME type from filename extension 如何从文件扩展名中获取MIME类型? #1楼 参考:https://stackoom.com/question/ ...
- (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致...
1.打开注册表编辑器 方法:开始 -> 运行 -> 输入regedit -> 确定2.找到注册表子项 HKEY_CURRENT_USER\Software\Microsoft\Off ...
- 根据文件扩展名得到文件对应该类型Icon方法
2019独角兽企业重金招聘Python工程师标准>>> 根据文件扩展名得到文件对应该类型Icon方法 package com.fleety.util; import java.awt ...
- 如何自动备份指定文件扩展名的文件?
关于文件扩展名 文件扩展名,一个点后跟几个字母,例如".doc"或".jpg",构成计算机文档名称的结尾.保存文档时,请务必在单击"保存"之 ...
- 已解决Excel无法打开文件test.xIsx“,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。
已解决Excel无法打开文件test.xIsx",因为文件格式或文件扩展名无效.请确定文件未损坏,并且文件扩展名与文件的格式匹配. 文章目录 报错代码 报错原因 解决方法 帮忙解决 报错代码 ...
- Excel无法打开文件xxx.xlsx,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配...
office版本:2016 系统版本:win10 问题描述: 1.桌面新建excel表格后,打开时,提示"Excel无法打开文件xxx.xlsx,因为文件格式或文件扩展名无效.请确定文件 ...
- Excel无法打开文件xxx.xlsx,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配
office版本:2016 系统版本:win10 问题描述: 1.桌面新建excel表格后,打开时,提示"Excel无法打开文件xxx.xlsx,因为文件格式或文件扩展名无效.请确定文件未损 ...
- 打开excel显示php拓展名,phpexcel 导出excel 因为文件格式或文件扩展名无效,请确定文件未损坏,并且文件扩展名与文件的格式匹配...
phpexcel导出excel:打开出现这个错误,强制打开是乱码 $objPHPExcel =newPHPExcel(); $filename ="test.xls"; heade ...
- NPOI 导入导出和Excel版本,错误文件扩展名和文件的格式不匹配
读取时可以自动判断Excel版本 IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs); 调用这个方法,内部自动判断Exc ...
最新文章
- 修复Debian grub
- gcc/g++命令参数笔记
- 每次ubuntu12.04重启后,/etc/resolv.conf被重写为空或127.0.0.1
- PHP易混淆函数的区分方法及意义
- 定时执行 Job - 每天5分钟玩转 Docker 容器技术(135)
- 关于win32与win64的兼容性问题
- android一体机-迅为10.1寸用于售货机、人机界面、自动终端、触摸控制
- 2021高考无准考证成绩查询,2021考研没有准考证号怎么查成绩
- asp.net treeview 控件父子节点级联选中
- Apache HttpClient4使用教程
- 两阶段最小二乘法与R
- 微信公众帐号开发教程第8篇-QQ表情的发送与接收
- iOS 客户端 IM 以及列表 UI 框架
- linux定期清理日志脚本,一周清理一次
- 基于51的双机通信系统
- html期末成绩查询页面,小学分数查询
- 电商前台项目(五):完成加入购物车功能和购物车页面
- oracle 如何导入txt,Oracle中导入TXT并进行处理
- EtherCAT总线伺服电机/一体化伺服电机如何清零当前位置
- 12.贝叶斯正则化,在线学习,误差分析,销蚀分析