C# 通过文件扩展名获取图标和描述


C# 通过文件扩展名获取图标和描述
AttachTool 类源码:

贴图图片

C# Code:

/// <summary>
/// 通过扩展名得到图标和描述
/// </summary>
/// <param name="ext">扩展名</param>
/// <param name="LargeIcon">得到大图标</param>
/// <param name="smallIcon">得到小图标</param>
public static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description)
{
   largeIcon = smallIcon = null;
   description = "";
   var extsubkey = Registry.ClassesRoot.OpenSubKey(ext); //从注册表中读取扩展名相应的子键
   if (extsubkey != null)
   {
      var extdefaultvalue = (string)extsubkey.GetValue(null); //取出扩展名对应的文件类型名称
      
      //未取到值,返回预设图标
      if (extdefaultvalue == null)
      {
         GetDefaultIcon(out largeIcon, out smallIcon);
         return;
      }
      
      var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue); //从注册表中读取文件类型名称的相应子键
      if (typesubkey != null)
      {
         description = (string)typesubkey.GetValue(null); //得到类型描述字符串
         var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon"); //取默认图标子键
         if (defaulticonsubkey != null)
         {
            //得到图标来源字符串
            var defaulticon = (string)defaulticonsubkey.GetValue(null); //取出默认图标来源字符串
            var iconstringArray = defaulticon.Split(',');
            int nIconIndex = 0;
            if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex);
            //得到图标
            System.IntPtr phiconLarge = new IntPtr();
            System.IntPtr phiconSmall = new IntPtr();
            ExtractIconExW(iconstringArray[0].Trim('"'), nIconIndex, ref phiconLarge, ref phiconSmall, 1);
            if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
            if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
         }
      }
   }
}





C# Code:

/// <summary>
/// 获取缺省图标
/// </summary>
/// <param name="largeIcon"></param>
/// <param name="smallIcon"></param>
public static void GetDefaultIcon(out Icon largeIcon, out Icon smallIcon)
{
   largeIcon = smallIcon = null;
   System.IntPtr phiconLarge = new IntPtr();
   System.IntPtr phiconSmall = new IntPtr();
   ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 0, ref phiconLarge, ref phiconSmall, 1);
   if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
   if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:

/// Return Type: UINT->unsigned int
///lpszFile: LPCWSTR->WCHAR*
///nIconIndex: int
///phiconLarge: HICON*
///phiconSmall: HICON*
///nIcons: UINT->unsigned int
[DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static extern uint ExtractIconExW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, uint nIcons);

public static void CreateFileIcon(string fileType, out Icon large, out Icon small)
{
   string des;
   
   if (fileType.Trim() == "") //预设图标
   {
      AttachTool.GetDefaultIcon(out large, out small);
   }
   else if (fileType.ToUpper() == ".EXE") //应用程序图标单独获取
   {
      IntPtr l = IntPtr.Zero;
      IntPtr s = IntPtr.Zero;
      
      AttachTool.ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 2, ref l, ref s, 1);
      
      large = Icon.FromHandle(l);
      small = Icon.FromHandle(s);
   }
   else //其它类型的图标
   {
      AttachTool.GetExtsIconAndDescription(fileType, out large, out small, out des);
   }
   
   if ((large == null) || (small == null)) //无法获取图标,预设图标
   AttachTool.GetDefaultIcon(out large, out small);
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:

public static byte[] ImageToByte(Image image)
{
   MemoryStream ms = new MemoryStream();
   image.Save(ms, ImageFormat.Bmp);
   byte[] bs = ms.ToArray();
   ms.Close();
   return bs;
}

public static Image ByteToImage(byte[] bs)
{
   MemoryStream ms = new MemoryStream(bs);
   Bitmap bmp = new Bitmap(ms);
   ms.Close();
   return bmp;
}


//来源:C/S框架网(www.csframework.com) QQ:1980854898




C# Code:


[StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct HICON__
{
   public int unused;
}

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:SQL语句前面加 N 是什么意思?
下一篇:CS开发框架提供那些模板窗体?
评论列表

发表评论

评论内容
昵称:
关联文章

C# 通过文件扩展名获取图标描述
根据文件扩展名获取图片编码器如jpg/png格式
UDDI 统一描述、发现集成
VS Winform设置应用程序文件图标及任务栏显示图标|C/S框架网
[原创]C#一键隐藏QQ/MSN,显示/隐藏系统托盘图标,获取托盘图标
ASP.NET通过HttpContext获取IP地址
C# 几个常用获取当前文件名的方法
C#获取应用程序当前文件目录位置(安装路径)
C#获取系统嵌入式资源,如Xml,Txt文件
WSDL Web服务描述语言
C# getset访问器:获取设置字段(属性)的值
C#获取内网外网IP
C# 从注册表获取Windows服务的文件路径
DevExpress 通过小三角图标变更控件类型
JavaScrip JS获取Image图片实际宽度高度
C#获取文件的编码格式(UTF-8/UNICODE/ASCII)
C#获取文本文件的编码格式Encoding,自动区分GB2312UTF8
不使用配置文件构建使用WCF服务(WCF Service)
C# DataSetDataTable
C# Image图片转Icon图标

热门标签
.NET5 .NET6 .NET7 APP Auth-软件授权注册系统 Axios B/S B/S开发框架 Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 DAL数据访问层 Database datalock DbFramework Demo教学 Demo下载 DevExpress教程 DOM EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS NavBarControl Node.JS NPM OMS ORM PaaS POS Promise API Redis SAP SEO SQL SQLConnector TMS系统 Token令牌 VS2022 VSCode VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 版本区别 报表 踩坑日记 操作手册 代码生成器 迭代开发记录 基础资料窗体 架构设计 角色权限 开发sce 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 秘钥 密钥 权限设计 软件报价 软件测试报告 软件简介 软件开发框架 软件开发平台 软件开发文档 软件体系架构 软件下载 软著证书 三层架构 设计模式 生成代码 实用小技巧 收钱音箱 数据锁 数据同步 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序