1. 程式人生 > >獲取資料夾中的圖示資源

獲取資料夾中的圖示資源

實現效果:

  

知識運用:

  API函式SHGetFileInfo    //獲取包含在可執行檔案或Dll中的圖示數或圖示資源

  [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
  public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);

  

  和ExtractIconEx函式      //從限定的可執行檔案 動態連結庫 或者圖示檔案中生成圖示控制代碼陣列

  [DllImport("shell32.dll")]
  public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);

  

實現程式碼:

        [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);
        [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
        public static extern int DestroyIcon(IntPtr hIcon);
        [DllImport("shell32.dll")]
        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
        [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;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
                GetlistViewItem(textBox1.Text,imageList1,lv1);
        }

        public void GetlistViewItem(string path, ImageList imagelist, ListView lv)      //獲取指定路徑下的所有檔案及其圖示
        {
            lv.Items.Clear();
            SHFILEINFO shfi = new SHFILEINFO();                                         //建立SHFILEINFO物件
            try
            {
                string[] dirs = Directory.GetDirectories(path);                         //獲取指定目錄中子目錄的名稱
                string[] files = Directory.GetFiles(path);                              //獲取指定路徑中檔案的名稱
                for (int i = 0; i < dirs.Length; i++)                                   //遍歷子資料夾
                {
                    string[] info = new string[4];                                      //定義一個數組
                    DirectoryInfo dir = new DirectoryInfo(dirs[i]);                     //根據資料夾路徑建立DirectoryInfo物件
                    if (!(dir.Name == "RECYCLER" || dir.Name == "RECYCLED" || dir.Name == "Recycled" || dir.Name == "System Volume Infomation"))
                    {
                        //獲取資料夾的圖示及型別
                        SHGetFileInfo(dirs[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400));
                        imagelist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());  //新增圖示
                        info[0] = dir.Name;                                             //獲取資料夾名稱
                        info[1] = "";                                                   //獲取資料夾大小
                        info[2] = "資料夾";                                             //獲取型別
                        info[3] = dir.LastAccessTime.ToString();                        //獲取修改時間
                        ListViewItem item = new ListViewItem(info,dir.Name);            //建立ListViewItem物件
                        lv.Items.Add(item);                                             //添加當前資料夾的基本資訊
                        DestroyIcon(shfi.hIcon);                                        //銷燬圖示
                    }

                }
                for (int i = 0; i < files.Length; i++)                                  //遍歷目錄下的檔案
                {
                    string[] info = new string[4];
                    FileInfo fi=new FileInfo(files[i]);
                    string Filetype=files[i].Substring(files[i].LastIndexOf(".")+1,files[i].Length-files[i].LastIndexOf(".")-1);
                    string Newtype=Filetype.ToString();
                    if (!(Newtype == "sys" || Newtype == "ini" || Newtype == "bin" || Newtype ==  "log" || Newtype == "com" || Newtype == "bat" || Newtype == "db"))
                    {
                        SHGetFileInfo(files[i], (uint)0x80, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), (uint)(0x100 | 0x400));
                        imagelist.Images.Add(fi.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
                        info[0] = fi.Name;
                        info[1] = fi.Length.ToString();
                        info[2] = fi.Extension.ToString();
                        info[3] = fi.LastAccessTime.ToString();
                        ListViewItem item = new ListViewItem(info, fi.Name);
                        lv.Items.Add(item);
                        DestroyIcon(shfi.hIcon);
                    }
                }
            }
            catch{}
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }