您的位置:首页 > 其它

根据文件名或文件扩展名获取文件的默认图标

2016-04-01 15:08 155 查看
新建一个vs2010 窗体项目,新建按钮button和图片picturebox
下面是程序

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 image= Icon.ExtractAssociatedIcon(Filepath).ToBitmap();//转换一下
Icon icon = Icon.ExtractAssociatedIcon(pathxx);

return icon;
}
}
}

根据不同的文件扩展名显示不同的图标,在C#中可以使用 Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(fileFullName) 来得到指定文件图标。
/// <summary>
/// 依据文件名读取图标,若指定文件不存在,则返回空值。
/// </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>
/// 根据文件扩展名(如:.*),返回与之关联的图标。
/// 若不以"."开头则返回文件夹的图标。
/// </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;
}
}

虽然在C#中可以使用 Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(fileFullName) 来得到指定文件图标。但是Icon.ExtractAssociatedIcon 只能获取大图标,要获取小图标还是要使用 API。

using System;
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
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: