欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ListView 根据 文件路径 或 扩展名 显示系统文件图标

程序员文章站 2022-07-11 08:08:43
private void 获取文件button1_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); string path = folderBrowserDialog1.SelectedPath; listV ......

 private void 获取文件button1_click(object sender, eventargs e)

        {
            folderbrowserdialog1.showdialog();
            string path = folderbrowserdialog1.selectedpath;

            listview1.view = view.list;
            listview1.items.clear();
 
            directoryinfo dir = new directoryinfo(path);
 
            #region 递归遍历文件夹
            foreach (directoryinfo dirfolder in dir.getdirectories())
            {
                listview1.smallimagelist = imagelist1;
                listview1.items.add(dirfolder.name, 0);
            }
            #endregion
 
            #region 递归遍历文件;
            foreach (fileinfo dirfile in dir.getfiles())
            {
                // listview1.items.add(dirfile.name, 1);
                if (!imagelist1.images.keys.contains(dirfile.extension))
                {
                    imagelist1.images.add(dirfile.extension,icon.extractassociatedicon(dirfile.fullname));
                }
                listviewitem lvi = new listviewitem();
                lvi.text = dirfile.name;
                lvi.imageindex = imagelist1.images.keys.indexof(dirfile.extension);
                listview1.items.add(lvi);
             }
             #endregion
        }