C# 获取系统Icon、获取文件相关的Icon

简介: 原文:C# 获取系统Icon、获取文件相关的Icon 1、获取系统Icon 工具下载SystemIcon.exe using System; using System.Collections.

原文:C# 获取系统Icon、获取文件相关的Icon

1、获取系统Icon 工具下载SystemIcon.exe

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、获取文件相关的Icon

using 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);
        }
    }
}

  


目录
相关文章
|
5月前
|
SQL 数据库 数据安全/隐私保护
C#wpf学习卡后台管理系统
C#wpf学习卡后台管理系统
125 32
|
4月前
|
存储 监控 算法
基于 C# 的局域网计算机监控系统文件变更实时监测算法设计与实现研究
本文介绍了一种基于C#语言的局域网文件变更监控算法,通过事件驱动与批处理机制结合,实现高效、低负载的文件系统实时监控。核心内容涵盖监控机制选择(如事件触发机制)、数据结构设计(如监控文件列表、事件队列)及批处理优化策略。文章详细解析了C#实现的核心代码,并提出性能优化与可靠性保障措施,包括批量处理、事件过滤和异步处理等技术。最后,探讨了该算法在企业数据安全监控、文件同步备份等场景的应用潜力,以及未来向智能化扩展的方向,如文件内容分析、智能告警机制和分布式监控架构。
121 3
|
5月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
180 0
|
9月前
|
C#
基于 C# 编写的 Visual Studio 文件编码显示与修改扩展插件
基于 C# 编写的 Visual Studio 文件编码显示与修改扩展插件
169 9
|
9月前
|
存储 监控 算法
企业内网监控系统中基于哈希表的 C# 算法解析
在企业内网监控系统中,哈希表作为一种高效的数据结构,能够快速处理大量网络连接和用户操作记录,确保网络安全与效率。通过C#代码示例展示了如何使用哈希表存储和管理用户的登录时间、访问IP及操作行为等信息,实现快速的查找、插入和删除操作。哈希表的应用显著提升了系统的实时性和准确性,尽管存在哈希冲突等问题,但通过合理设计哈希函数和冲突解决策略,可以确保系统稳定运行,为企业提供有力的安全保障。
|
11月前
|
存储 开发框架 .NET
C#语言如何搭建分布式文件存储系统
C#语言如何搭建分布式文件存储系统
258 2
|
11月前
|
存储 C#
【C#】大批量判断文件是否存在的两种方法效率对比
【C#】大批量判断文件是否存在的两种方法效率对比
289 1
|
11月前
|
存储 分布式计算 监控
C# 创建一个分布式文件存储系统需要怎么设计??
C# 创建一个分布式文件存储系统需要怎么设计??
151 0
|
11月前
|
XML 存储 缓存
C#使用XML文件的详解及示例
C#使用XML文件的详解及示例
446 0
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
942 2