初学C#,最近在做一个小作业——文件管理器。
打开文件夹后在listview里面显示文件夹里的文件。怎样根据每个文件的类型获得对应的图标,并显示呢?
有没有那个仁兄做过这样的程序,,给小妹参考参考吖!!

解决方案 »

  1.   

    windows提供了api可以获取文件信息,其中有一项就是文件的图标;至于显示嘛,你把图片添加到imagelist中,然后指定相应文件结点的imageindex就可以显示图标了;
      

  2.   


    string strFile = @"c:\abc\e.jpg";
    string strEx = System.IO.Path.GetExtension(strFile); //  返回 .jpg
    string strName = System.IO.Path.GetFileName(strFile); // 返回 e.jpg
    根据后缀名就行了,既然是作业,就不用搞那么复杂
      

  3.   

    “把图片添加到imagelist中,然后指定相应文件结点的imageindex就可以显示图标了;”这个我会,我在另一个listview里面显示子文件夹的时候就用到了,每个子文件夹旁边弄了个文件夹的小图标。
    但是不知道文件的不同格式不同图标我就不会了。
    主要是这个api怎么用啊??
      

  4.   

    嗯嗯嗯,就用这个,先获得后缀名,再用switch,不同的后缀名就用不同的图标。。谢谢啊!!!虽说是作业,可这作业不同在学校的作业,,悲惨!!
    他想让我通过做这个文件管理程序来学习C#,要求的功能我之前就做好了,验收了,现在要我继续在这上面添加和优化功能。等到这块做到没东西做了,学得差不多了,再用别的作业来学习!!
      

  5.   

    [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
            public static extern int GetFileInfo(string pszPath, int dwFileAttributes, ref IconFileInfor psfi, int cbFileInfo, int uFlags);
    获取文件信息,包括图标,文件显示的是什么图标,就会获取什么图标,和扩展名没有关系;
      

  6.   


    获得的这个信息不知道怎么用。。比如说怎样在listview里面显示这些呢??
      

  7.   

    这个只能取文件的信息,你取到后,把image加到你的imagelist中就可以用了吧?
      

  8.   


    能不能来一段代码看看?咦??图片不是在imagelist的images属性里面添加的吗??
    初学者啊,而且资质也不够好,不懂得东西太多了,不要笑话我才是。
      

  9.   

    this.listView1.Items[i].ImageIndex =1  
    或添加图标列
    文件图标
    [DllImport("shell32")]  
      private static extern int ExtractIcon(int hinst, string lpszExeFileName, int nIconIndex);  int icons = ExtractIcon(0, Application.ExecutablePath, 0); 
    http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx
      

  10.   

    看看这位老大的,很简单;其实就是应用win32 api取图片而已;然后把图标放到imagelist中去;
      

  11.   

    嗯, 不要想着自己找图标
    问 shell 要图标
      

  12.   

    我现在感觉很晕!!越来越多不懂得了,,,哎~~
    我用了很笨的方法。        
          public int GetImage(string file)
            {
                string fileEx = Path.GetExtension(file);
                switch (fileEx)
                {
                    case ".doc":
                    case ".docx":
                        return 1;                    
                    case ".jpg":
                    case ".JPG":
                        return 2;                    
                    case ".png":
                    case ".PNG":
                        return 3;                    
                    case ".ppt":
                    case ".pptx":
                    case ".PPT":
                        return 4;                    
                    case ".txt":
                    case ".TXT":
                        return 5;                    
                    case ".xls":
                    case ".xlsx":
                        return 6;                    
                    case ".rar":
                    case ".RAR":
                        return 7;                    
                    case ".exe":
                    case ".EXE":
                        return 8;                    
                    case ".bmp":
                    case ".BMP":
                        return 9;                    
                    default:
                        return 10;                    
                }
            }
    事先把这几种类型的图片添加到imagelist中了。返回了要用的索引值。出了这几文件全都用不知道格式的图标8。。我不想这样,因为这样的能用的图标太少了。
      

  13.   

    图标可以用上面几位提到的API获取,用文件的扩展名作为key,用IamgeList.Images.Add(string key, Image image)方法添加到ImageList中。
    每次有一个文件的时候先根据扩展名判断ImageList中有没有图标,如果没有就用API取到图标,放到ImageList中,如果有就直接用。
    碰到有大写有小写字母的时候你可以都转成小写或大写。
      

  14.   


    我觉得自己真的有够菜鸟的了,,大侠,您能不能给一段代码试试,,那个API怎么用,我还是琢磨不出来~~
      

  15.   


    using System.Runtime.InteropServices;
    public static uint SHGFI_ICON = 0x100;                     
    public static uint SHGFI_DISPLAYNAME = 0x200;              
    public static uint SHGFI_TYPENAME = 0x400;                 
    public static uint SHGFI_ATTRIBUTES = 0x800;               
    public static uint SHGFI_ICONLOCATION = 0x1000;            
    public static uint SHGFI_EXETYPE = 0x2000;                  
    public static uint SHGFI_SYSICONINDEX = 0x4000;            
    public static uint SHGFI_LINKOVERLAY = 0x8000;             
    public static uint SHGFI_SELECTED = 0x10000;               
    public static uint SHGFI_LARGEICON = 0x0;                  
    public static uint SHGFI_SMALLICON = 0x1;                  
    public static uint SHGFI_OPENICON = 0x2;                   
    public static uint SHGFI_SHELLICONSIZE = 0x4;              
    public static uint SHGFI_PIDL = 0x8;                       
    public static uint SHGFI_USEFILEATTRIBUTES = 0x10;         
    public static uint FILE_ATTRIBUTE_NORMAL = 0x80;
    public static uint LVM_FIRST = 0x1000;
    public static uint LVM_SETIMAGELIST = LVM_FIRST + 3;
    public static uint LVSIL_NORMAL = 0;
    public static uint LVSIL_SMALL = 1;
    [DllImport("Shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath,
        uint dwFileAttributes, ref SHFILEINFO psfi,
        int cbfileInfo, uint uFlags);
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public int iIcon;
        public int dwAttributes;
        public string szDisplayName;
        public string szTypeName;
    }
    [DllImport("User32.DLL")]
    public static extern int SendMessage(IntPtr hWnd,
        uint Msg, IntPtr wParam, IntPtr lParam);
    public void ListViewSysImages(ListView AListView)
    {
        SHFILEINFO vFileInfo = new SHFILEINFO();
        IntPtr vImageList = SHGetFileInfo("", 0, ref vFileInfo,
            Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
            SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
        SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_NORMAL,
            vImageList);
        vImageList = SHGetFileInfo("", 0, ref vFileInfo,
            Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
            SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
       SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_SMALL,
            vImageList);
    }
    public int FileIconIndex(string AFileName)
    {
        SHFILEINFO vFileInfo = new SHFILEINFO();
        SHGetFileInfo(AFileName, 0, ref vFileInfo,
            Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX);
        return vFileInfo.iIcon;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        ListViewSysImages(listView1);
        listView1.Items.Add("temp.txt", FileIconIndex(@"c:\temp\temp.txt"));
    }
      

  16.   

    // Get file info
    FileInfo f = new FileInfo("myfile.name");
    // Get icon for fileinfo!!!!!!!!!!!!!!!!
    Icon ico = Icon.ExtractAssociatedIcon(f);
    // Convert icon to bitmap
    Bitmap bm = ico.ToBitmap();
    // create new image with desired size
    Bitmap img = new Bitmap(16,16,PixelFormat.Frmat32bpRgb);
    // Create graphics with desired sized image
    Graphics g = Graphics.FormImage(img);
    // set interpolation mode
    g.InterpolationMode = InterpolationMode.HighQualityBiCubic;
    // draw/resize image
    g.DrawImage(bm, new Rectangle(0,0,16,16), new Rectangle(0, 0, bm.Width, bm,Height), GraphicalUnit.Pixel);
    // Paste to clipboard
    Clipboard.SetImage(bm);
    // Paste in RichtextBox
    rtb.Paste();