如题!谢谢

解决方案 »

  1.   

    switch (ext.ToLower()){   
          case "txt":   
              currentTreeNode.ImageUrl = "images/ext_txt.gif";   
              break;   
          case "gif":   
              currentTreeNode.ImageUrl = "images/ext_gif.gif";   
              break;   
          ...   
    }  
      

  2.   

    这个应该是事先准备好相关的图片的,即使是Windows操作系统,在没有安装相关软件的时候,文件的图标也是显示一个未知文件类型的图标楼主不会想要“牛”到所有的文件类型都能正确显示其图标吧
      

  3.   

    网上上的一个WPF的demo,就是实现这个功能的,lz可以看看代码
    http://files.cnblogs.com/tengs2000/IconUtils.zip
      

  4.   

    using System;
    using System.Runtime.InteropServices;
    using System.Drawing;namespace MyNamespace
    {
        public class FileIcon
        {
            /// <summary>
            ///  获取文件的默认图标
            /// </summary>
            /// <param name="fileName">文件名。
            ///     可以只是文件名,甚至只是文件的扩展名(.*);
            ///     如果想获得.ICO文件所表示的图标,则必须是文件的完整路径。
            /// </param>
            /// <param name="largeIcon">是否大图标</param>
            /// <returns>文件的默认图标</returns>
            public static Icon GetFileIcon(string fileName, bool largeIcon)
            {
                SHFILEINFO info = new SHFILEINFO(true);
                int cbFileInfo = Marshal.SizeOf(info);
                SHGFI flags;
                if (largeIcon)
                    flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
                else
                    flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;            SHGetFileInfo(fileName, 256, out info, (uint) cbFileInfo, flags);
                return Icon.FromHandle(info.hIcon);
            }
            [DllImport("Shell32.dll")]
            private static extern int SHGetFileInfo
              (
              string pszPath,
              uint dwFileAttributes,
              out   SHFILEINFO psfi,
              uint cbfileInfo,
              SHGFI uFlags
              );        [StructLayout(LayoutKind.Sequential)]
            private struct SHFILEINFO
            {
                public SHFILEINFO(bool b)
                {
                    hIcon = IntPtr.Zero;
                    iIcon = 0;
                    dwAttributes = 0;
                    szDisplayName = "";
                    szTypeName = "";
                }
                public IntPtr hIcon;
                public int iIcon;
                public uint dwAttributes;
                [MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
                public string szDisplayName;
                [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
                public string szTypeName;
            };        private enum SHGFI
            {
                SmallIcon = 0x00000001,
                LargeIcon = 0x00000000,
                Icon = 0x00000100,
                DisplayName = 0x00000200,
                Typename = 0x00000400,
                SysIconIndex = 0x00004000,
                UseFileAttributes = 0x00000010
            }
        }
    }
    以上代码可以实现你要的功能,不过嘛,遍历并呈现在web上并不是啥好方法,在web上应该尽量使用轻量级的东西,为了一个无伤大雅的小功能而无端的耗时间不划算啊
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            const uint SHGFI_ICON = 0x100;
            const uint SHGFI_USEFILEATTRIBUTES = 0x10;        [DllImport("Shell32", CharSet = CharSet.Auto)]
            internal extern static int ExtractIconEx(
                [MarshalAs(UnmanagedType.LPTStr)] 
    string lpszFile,       //size of the icon
                int nIconIndex,        //index of the icon 
                //(in case we have more 
                //then 1 icon in the file
                IntPtr[] phIconLarge,  //32x32 icon
                IntPtr[] phIconSmall,  //16x16 icon
                int nIcons);           //how many to get        [DllImport("shell32.dll")]
            static extern IntPtr SHGetFileInfo(
                string pszPath,                //path
                uint dwFileAttributes,        //attributes
                ref SHFILEINFO psfi,        //struct pointer
                uint cbSizeFileInfo,        //size
                uint uFlags);    //flags        //we need this function to release the unmanaged resource,
            //the unmanaged resource will be 
            //copies to a managed one and it will be returned.
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            extern static bool DestroyIcon(IntPtr handle);
            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;
            };
            public enum IconSize : uint
            {
                Large = 0x0,  //32x32
                Small = 0x1 //16x16        
            }
            public static Icon IconFromExtension(string Extension,
                IconSize Size)
            {
                try
                {
                    Icon TempIcon;                //add '.' if nessesry
                    if (Extension[0] != '.')
                        Extension = '.' + Extension;                //temp struct for getting file shell info
                    SHFILEINFO TempFileInfo = new SHFILEINFO();                SHGetFileInfo(
                        Extension,
                        0,
                        ref TempFileInfo,
                        (uint)Marshal.SizeOf(TempFileInfo),
                        SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size);                TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon);
                    return GetManagedIcon(ref TempIcon);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("error" +
                        " while trying to get icon for " +
                        Extension + " :" + e.Message);
                    return null;
                }
            }
            public static Icon GetManagedIcon(ref Icon UnmanagedIcon)
            {
                Icon ManagedIcon = (Icon)UnmanagedIcon.Clone();            DestroyIcon(UnmanagedIcon.Handle);            return ManagedIcon;
            }
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                System.Drawing.Icon icon = IconFromExtension("txt", IconSize.Large);
                this.Icon = icon;
            }
        }
    }
      

  6.   

    看看能不能找到系统图标的存放位置,不行的话看看能不能调系统api
      

  7.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    using System.Runtime.InteropServices;public partial class _Default : System.Web.UI.Page 
    {
        const uint SHGFI_ICON = 0x100;
            const uint SHGFI_USEFILEATTRIBUTES = 0x10;        [DllImport("Shell32", CharSet = CharSet.Auto)]
            internal extern static int ExtractIconEx(
                [MarshalAs(UnmanagedType.LPTStr)] 
                string lpszFile,       //size of the icon
                int nIconIndex,        //index of the icon 
                //(in case we have more 
                //then 1 icon in the file
                IntPtr[] phIconLarge,  //32x32 icon
                IntPtr[] phIconSmall,  //16x16 icon
                int nIcons);           //how many to get        [DllImport("shell32.dll")]
            static extern IntPtr SHGetFileInfo(
                string pszPath,                //path
                uint dwFileAttributes,        //attributes
                ref SHFILEINFO psfi,        //struct pointer
                uint cbSizeFileInfo,        //size
                uint uFlags);    //flags        //we need this function to release the unmanaged resource,
            //the unmanaged resource will be 
            //copies to a managed one and it will be returned.
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            extern static bool DestroyIcon(IntPtr handle);
            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;
            };
            public enum IconSize : uint
            {
                Large = 0x0,  //32x32
                Small = 0x1 //16x16        
            }
            public static Icon IconFromExtension(string Extension,
                IconSize Size)
            {
                try
                {
                    Icon TempIcon;                //add '.' if nessesry
                    if (Extension[0] != '.')
                        Extension = '.' + Extension;                //temp struct for getting file shell info
                    SHFILEINFO TempFileInfo = new SHFILEINFO();                SHGetFileInfo(
                        Extension,
                        0,
                        ref TempFileInfo,
                        (uint)Marshal.SizeOf(TempFileInfo),
                        SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size);                TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon);
                    return GetManagedIcon(ref TempIcon);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("error" +
                        " while trying to get icon for " +
                        Extension + " :" + e.Message);
                    return null;
                }
            }
            public static Icon GetManagedIcon(ref Icon UnmanagedIcon)
            {
                Icon ManagedIcon = (Icon)UnmanagedIcon.Clone();            DestroyIcon(UnmanagedIcon.Handle);            return ManagedIcon;
            }    protected void Page_Load(object sender, EventArgs e)
        {
            Icon icon = IconFromExtension("rar", IconSize.Large);
            using(System.IO.MemoryStream ms=new System.IO.MemoryStream())
            {
                icon.ToBitmap().Save(this.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Close();
            }
        }
    }