使用API获取Windows系统的文件关联图标
来源:互联网 发布:有故事的淘宝店铺 编辑:程序博客网 时间:2024/06/05 05:38
#region 读取文件图标
[DllImport("shell32.dll")]
static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
/// <summary>
/// 给出文件扩展名(.*),返回相应图标
/// </summary>
/// <param name="fileType">文件类型</param>
/// <param name="isLarge">是否需要大图标,bool值</param>
/// <returns></returns>
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.Length <= 0)
{
//没有读取到文件类型注册信息,指定为未知文件类型的图标
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 = 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;
}
- 使用API获取Windows系统的文件关联图标
- pyhton 获取windows文件关联的图标
- 用VC获取文件的关联图标
- 用VC获取文件的关联图标
- 获取与文件扩展名关联的图标
- C# 获取系统关联文件图标完整类
- C# 获取系统关联文件图标完整类
- vc 获取系统关联图标
- C#获取文件(磁盘驱动器)的关联图标、获取目录文件和图标
- 获取系统文件图标
- 如何获取文件的系统图标
- delphi 获取系统注册的文件图标
- 获取文件的系统图标并保存
- Windows 系统使用的各种图标、光标
- 文件类型关联的文件图标
- 获取系统信息的有关Windows API
- 使用VC取得文件关联图标的方法
- 使用VC取得文件关联图标的方法
- 优势互补 云计算与虚拟化结合技术分析(引帖)
- 在Android源码的JavaScriptCore引擎中添加LOG
- 终于解决了“在添加新项时选择SQL时提示说“与SQL Server文件(*.mdf)的连接要求安装SQL Server2005才能正常工作,请确认是否安装了该组件,……”
- IO输入输出 学习
- 链表源码之双向链表
- 使用API获取Windows系统的文件关联图标
- False positive and False negative (误报 漏报)
- Hive 入门
- hibernate one-to-many/many-to-one 使用中间表/使用join 标签
- 30个漂亮网站
- [USACO] OPEN11 Bronze Division
- C#中打开新窗体关闭旧窗体
- LINUX进程控制 笔记
- 原来如此