程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#讀取系統文件圖標

C#讀取系統文件圖標

編輯:C#入門知識

using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace TOA.Common
{
 /// <summary>
 /// GetIcon 的摘要說明。
 /// </summary>
 public class ShellImages
 {

  #region DLLIMPORT
  // Retrieves information about an object in the file system,
  // such as a file, a folder, a directory, or a drive root.
  [DllImport("shell32",
    EntryPoint = "SHGetFileInfo",
    ExactSpelling = false,
    CharSet = CharSet.Auto,
    SetLastError = true)]
  private static extern IntPtr SHGetFileInfo(
   string pszPath,      //指定的文件名
   FILE_ATTRIBUTE dwFileAttributes, //文件屬性
   ref SHFILEINFO sfi,     //返回獲得的文件信息,是一個記錄類型
   int cbFileInfo,      //文件的類型名
   SHGFI uFlags);
  #endregion

  #region STRUCTS
  // Contains information about a file object
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  private 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;   //文件的類型名
  };
  #endregion

  #region Enums
  // Flags that specify the file information to retrieve with SHGetFileInfo
  [Flags]
  public enum SHGFI : uint
  {
   ADDOVERLAYS = 0x20,
   ATTR_SPECIFIED = 0x20000,
   ATTRIBUTES = 0x800,   //獲得屬性
   DISPLAYNAME = 0x200,  //獲得顯示名
   EXETYPE = 0x2000,
   ICON = 0x100,    //獲得圖標
   ICONLOCATION = 0x1000,
   LARGEICON = 0,    //獲得大圖標
   LINKOVERLAY = 0x8000,
   OPENICON = 2,
   OVERLAYINDEX = 0x40,
   PIDL = 8,
   SELECTED = 0x10000,
   SHELLICONSIZE = 4,
   SMALLICON = 1,    //獲得小圖標
   SYSICONINDEX = 0x4000,
   TYPENAME = 0x400,   //獲得類型名
   USEFILEATTRIBUTES = 0x10
  }
  // Flags that specify the file information to retrieve with SHGetFileInfo
  [Flags]
  public enum FILE_ATTRIBUTE
  {
   READONLY = 0x00000001,
   HIDDEN = 0x00000002,
   SYSTEM = 0x00000004,
   DIRECTORY = 0x00000010,
   ARCHIVE = 0x00000020,
   DEVICE = 0x00000040,
   NORMAL = 0x00000080,
   TEMPORARY = 0x00000100,
   SPARSE_FILE = 0x00000200,
   REPARSE_POINT = 0x00000400,
   COMPRESSED = 0x00000800,
   OFFLINE = 0x00001000,
   NOT_CONTENT_INDEXED = 0x00002000,
   ENCRYPTED = 0x00004000
  }
  #endregion

  #region Variables
  //保存小圖標列表
  private static System.Windows.Forms.ImageList smallImageList;
  //保存大圖標列表
  private static System.Windows.Forms.ImageList largeImageList;
  static ShellImages()
  {
   smallImageList = new System.Windows.Forms.ImageList();
   largeImageList = new System.Win

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved