获取系统图标

 

需要先声明的结构体:

 [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;
    }


    class Win32
    {
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
      
    }

使用时:

 listView1.Items.Clear();
  SmallImaList.Images.Clear();
  LargeImaList.Images.Clear();
  int nIndex = 0;
  SHFILEINFO shinfo = new SHFILEINFO();

  listView1.SmallImageList = SmallImaList;
  listView1.LargeImageList = LargeImaList;

  //获取文件的图标
  foreach (string file in Directory.GetFiles(path))
     {
         Win32.SHGetFileInfo(file, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
         Icon myIcon = Icon.FromHandle(shinfo.hIcon);
         this.SmallImaList.Images.Add(myIcon);

         Win32.SHGetFileInfo(file, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
         myIcon = Icon.FromHandle(shinfo.hIcon);
         this.LargeImaList.Images.Add(myIcon);

         FileInfo fi = new FileInfo(file);
         if (!fi.Attributes.ToString().Contains("Hidden"))
            {
               ListViewItem lvi = listView1.Items.Add(file, Path.GetFileName(file), nIndex++);
               lvi.SubItems.Add(Directory.GetLastWriteTime(file).ToString());
               lvi.SubItems.Add(Path.GetExtension(file));
            }
     }

 

    //获取文件夹的图标
       if (Directory.GetDirectories(dir).Length > 0)
            {
                listView1.Items.Clear();
                BigImage.Images.Clear();
                SmallImage.Images.Clear();
                clear = false;
                Win32.SHGetFileInfo(dir, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
                Icon myIcon = Icon.FromHandle(shinfo.hIcon);
                //获取系统的文件的小ico图标 并添加到imagelist中
                this.SmallImage.Images.Add(myIcon);
                Win32.SHGetFileInfo(dir, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
                myIcon = Icon.FromHandle(shinfo.hIcon);
                //获取系统的文件的大图标
                this.BigImage.Images.Add(myIcon);
                foreach (string str in Directory.GetDirectories(dir))
                {
                    lvi = listView1.Items.Add(str, Path.GetFileName(str), 0);
                    lvi.SubItems.Add(Directory.GetLastWriteTime(str).ToString("yyyy年MM月dd日"));
                    lvi.SubItems.Add("文件夹");
                    lvi.SubItems.Add(GetSize(str).ToString());
                }
            }

 

 

 

 

 

 

你可能感兴趣的:(职场,休闲,学习日志16)