C# 获取系统Icon、获取文件相关的Icon
2018-02-25 14:17
246 查看
1、获取系统Icon 工具下载SystemIcon.exe
![](https://gitee.com/scimence/FileExplorer/raw/master/files/SystemIcon.png)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileExplorer
{
/// <summary>
/// 系统Icon
/// 1、Get() 获取指定索引对应的系统icon
/// 2、Save() 保存所有系统图像
/// 3、Show() 显示所有系统Icon图像
/// </summary>
public partial class SystemIcon : Form
{
public SystemIcon()
{
InitializeComponent();
Show(this);
Save();
}
/// <summary>
/// 在form上显示所有系统icon图像
/// </summary>
public static void Show(Form form)
{
LoadSystemIcon();
FlowLayoutPanel flowLayout = new FlowLayoutPanel();
flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
flowLayout.AutoScroll = true;
for (int i = 0; i < SystemIconList.Count; i++)
{
PictureBox pic = new PictureBox();
pic.Size = new System.Drawing.Size(32, 32);
flowLayout.Controls.Add(pic);
Bitmap p = SystemIconList[i].ToBitmap();
pic.Image = p;
}
form.Controls.Add(flowLayout);
}
/// <summary>
/// 保存所有系统图像
/// </summary>
public static void Save()
{
LoadSystemIcon();
for (int i = 0; i < SystemIconList.Count; i++)
{
Bitmap p = SystemIconList[i].ToBitmap();
// 保存图像
string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
string filepath = path + (i + ".png");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!File.Exists(filepath)) p.Save(filepath);
}
}
/// <summary>
/// 获取指定索引对应的系统icon
/// </summary>
public static Icon Get(int index)
{
LoadSystemIcon();
return index < SystemIconList.Count ? SystemIconList[index] : null;
}
private static List<Icon> SystemIconList = new List<Icon>(); // 记录系统图标
//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//private static extern bool MessageBeep(uint type);
[DllImport("Shell32.dll")]
public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
private static IntPtr[] largeIcon;
private static IntPtr[] smallIcon;
/// <summary>
/// 获取所有系统icon图像
/// </summary>
private static void LoadSystemIcon()
{
if (SystemIconList.Count > 0) return;
largeIcon = new IntPtr[1000];
smallIcon = new IntPtr[1000];
ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000);
SystemIconList.Clear();
for (int i = 0; i < largeIcon.Length; i++)
{
try
{
Icon ic = Icon.FromHandle(largeIcon[i]);
SystemIconList.Add(ic);
}
catch (Exception ex)
{
break;
}
}
}
//private void LoadSystemIcon()
//{
// largeIcon = new IntPtr[1000];
// smallIcon = new IntPtr[1000];
// ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000);
// FlowLayoutPanel flowLayout = new FlowLayoutPanel();
// flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
// flowLayout.AutoScroll = true;
// for (int i = 0; i < largeIcon.Length; i++)
// {
// try
// {
// PictureBox pic = new PictureBox();
// pic.Size = new System.Drawing.Size(32, 32);
// flowLayout.Controls.Add(pic);
// Icon ic = Icon.FromHandle(largeIcon[i]);
// SystemIcon.Add(ic);
// Bitmap p = ic.ToBitmap();
// pic.Image = p;
// // 保存图像
// string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
// string filepath = path + (i + ".png");
// if (!Directory.Exists(path)) Directory.CreateDirectory(path);
// if (!File.Exists(filepath)) p.Save(filepath);
// }
// catch (Exception ex)
// {
// break;
// }
// }
// this.Controls.Add(flowLayout);
//}
}
}
2、获取文件相关的Iconusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
namespace FileExplorer
{
/// <summary>
/// 获取指定文件的Icon图像getIcon()、getIcon2()
/// </summary>
class FileIcon
{
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0; //大图标
private const uint SHGFI_SMALLICON = 0x1; //小图标
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon; //文件的图标句柄
public IntPtr iIcon; //图标的系统索引号
public uint dwAttributes; //文件的属性值
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;//文件的显示名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName; //文件的类型名
};
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
/// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon(string FilePath)
{
SHFILEINFO shinfo = new SHFILEINFO();
//FileInfo info = new FileInfo(FileName);
//大图标
SHGetFileInfo(FilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
Icon largeIcon = Icon.FromHandle(shinfo.hIcon);
//Icon.ExtractAssociatedIcon(FileName);
return largeIcon;
}
/// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon2(string FilePath)
{
return Icon.ExtractAssociatedIcon(FilePath);
}
}
}
![](https://gitee.com/scimence/FileExplorer/raw/master/files/SystemIcon.png)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileExplorer
{
/// <summary>
/// 系统Icon
/// 1、Get() 获取指定索引对应的系统icon
/// 2、Save() 保存所有系统图像
/// 3、Show() 显示所有系统Icon图像
/// </summary>
public partial class SystemIcon : Form
{
public SystemIcon()
{
InitializeComponent();
Show(this);
Save();
}
/// <summary>
/// 在form上显示所有系统icon图像
/// </summary>
public static void Show(Form form)
{
LoadSystemIcon();
FlowLayoutPanel flowLayout = new FlowLayoutPanel();
flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
flowLayout.AutoScroll = true;
for (int i = 0; i < SystemIconList.Count; i++)
{
PictureBox pic = new PictureBox();
pic.Size = new System.Drawing.Size(32, 32);
flowLayout.Controls.Add(pic);
Bitmap p = SystemIconList[i].ToBitmap();
pic.Image = p;
}
form.Controls.Add(flowLayout);
}
/// <summary>
/// 保存所有系统图像
/// </summary>
public static void Save()
{
LoadSystemIcon();
for (int i = 0; i < SystemIconList.Count; i++)
{
Bitmap p = SystemIconList[i].ToBitmap();
// 保存图像
string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
string filepath = path + (i + ".png");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!File.Exists(filepath)) p.Save(filepath);
}
}
/// <summary>
/// 获取指定索引对应的系统icon
/// </summary>
public static Icon Get(int index)
{
LoadSystemIcon();
return index < SystemIconList.Count ? SystemIconList[index] : null;
}
private static List<Icon> SystemIconList = new List<Icon>(); // 记录系统图标
//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//private static extern bool MessageBeep(uint type);
[DllImport("Shell32.dll")]
public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);
private static IntPtr[] largeIcon;
private static IntPtr[] smallIcon;
/// <summary>
/// 获取所有系统icon图像
/// </summary>
private static void LoadSystemIcon()
{
if (SystemIconList.Count > 0) return;
largeIcon = new IntPtr[1000];
smallIcon = new IntPtr[1000];
ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000);
SystemIconList.Clear();
for (int i = 0; i < largeIcon.Length; i++)
{
try
{
Icon ic = Icon.FromHandle(largeIcon[i]);
SystemIconList.Add(ic);
}
catch (Exception ex)
{
break;
}
}
}
//private void LoadSystemIcon()
//{
// largeIcon = new IntPtr[1000];
// smallIcon = new IntPtr[1000];
// ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000);
// FlowLayoutPanel flowLayout = new FlowLayoutPanel();
// flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
// flowLayout.AutoScroll = true;
// for (int i = 0; i < largeIcon.Length; i++)
// {
// try
// {
// PictureBox pic = new PictureBox();
// pic.Size = new System.Drawing.Size(32, 32);
// flowLayout.Controls.Add(pic);
// Icon ic = Icon.FromHandle(largeIcon[i]);
// SystemIcon.Add(ic);
// Bitmap p = ic.ToBitmap();
// pic.Image = p;
// // 保存图像
// string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
// string filepath = path + (i + ".png");
// if (!Directory.Exists(path)) Directory.CreateDirectory(path);
// if (!File.Exists(filepath)) p.Save(filepath);
// }
// catch (Exception ex)
// {
// break;
// }
// }
// this.Controls.Add(flowLayout);
//}
}
}
2、获取文件相关的Iconusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
namespace FileExplorer
{
/// <summary>
/// 获取指定文件的Icon图像getIcon()、getIcon2()
/// </summary>
class FileIcon
{
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0; //大图标
private const uint SHGFI_SMALLICON = 0x1; //小图标
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon; //文件的图标句柄
public IntPtr iIcon; //图标的系统索引号
public uint dwAttributes; //文件的属性值
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;//文件的显示名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName; //文件的类型名
};
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
/// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon(string FilePath)
{
SHFILEINFO shinfo = new SHFILEINFO();
//FileInfo info = new FileInfo(FileName);
//大图标
SHGetFileInfo(FilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
Icon largeIcon = Icon.FromHandle(shinfo.hIcon);
//Icon.ExtractAssociatedIcon(FileName);
return largeIcon;
}
/// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon2(string FilePath)
{
return Icon.ExtractAssociatedIcon(FilePath);
}
}
}
相关文章推荐
- C#获取系统文件及文件夹图标
- C#获取一个文件的相关信息
- 用C#和WMI获取逻辑驱动器详细信息(驱动器类型,文件系统,剩余空间等)
- C# 文件相关操作 读 写 追加 删除 获取等
- C#读取系统文件信息(二)——调API读取系统文件及系统盘Icon图标
- C# 获取计算机系统相关信息
- 在c#中过滤通过System.IO.Directory.GetDirectories 方法获取的是所有的子目录和文件中的系统隐藏的文件(夹)的方法
- C#复制或删除文件夹(及文件夹下所有子文件夹和文件)、获取文件所在文件夹、二进制读取文件相关
- C#获取逻辑驱动器文件系统
- asp.net C#获取程序文件相关信息
- C#获取系统文件、文件夹和磁盘驱动器图标
- 获取文件属性的文件系统相关函数
- c# 获取系统文件图标
- C# 获取系统文件图标
- C# 获取系统文件图标
- C#获取程序文件相关信息的方法
- c# 获取系统文件图标
- C#获取文件相关信息的方法
- asp.net C#获取程序文件相关信息
- C#获取系统文件、文件夹和磁盘驱动器图标