我知道 可以用     Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(openFileDialog1.FileName);得到一个已经存在的文件的系统图标.但是我想指定一个后缀后, 没有文件也能得到它的系统ico例如 输入"*.txt" 它就给我一个txt文件的图标

解决方案 »

  1.   

    你可以在程序的当前文件夹下创建个.txt文件,然后再获取这个临时文件的ICO,然后再删掉文件
      

  2.   

    winapi 的SHGetFileInfo  我也试过了 ,好像对 *.txt 这样的  也是不行
      

  3.   

    自己顶一下,另外追加一个问题 ,为什么我用ico.save 出来的图像 偏色很严重?
      

  4.   

    这里有代码,自己看下吧。Display pretty file icons in your ASP.NET applications with IconHandler
    http://mvolo.com/display-pretty-file-icons-in-your-aspnet-applications-with-iconhandler
      

  5.   


    他用的就是SHGetFileInfo 
      

  6.   

    我是路过学习的 之前写过一个读取ico滴东东 但效果不好
      

  7.   

            /// <summary>   
            /// 通过扩展名得到图标和描述   
            /// </summary>   
            /// <param name="ext">扩展名</param>   
            /// <param name="LargeIcon">得到大图标</param>   
            /// <param name="smallIcon">得到小图标</param>   
            static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description)
            {
                largeIcon = smallIcon = null;
                description = "";
                var extsubkey = Registry.ClassesRoot.OpenSubKey(ext);   //从注册表中读取扩展名相应的子键   
                if (extsubkey != null)
                {
                    var extdefaultvalue = (string)extsubkey.GetValue(null);     //取出扩展名对应的文件类型名称   
                    var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue);  //从注册表中读取文件类型名称的相应子键   
                    if (typesubkey != null)
                    {
                        description = (string)typesubkey.GetValue(null);   //得到类型描述字符串   
                        var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon");  //取默认图标子键   
                        if (defaulticonsubkey != null)
                        {
                            //得到图标来源字符串   
                            var defaulticon = (string)defaulticonsubkey.GetValue(null); //取出默认图标来源字符串   
                            var iconstringArray = defaulticon.Split(',');
                            int nIconIndex = 0;
                            if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex);
                            //得到图标   
                            System.IntPtr phiconLarge = new IntPtr();
                            System.IntPtr phiconSmall = new IntPtr();
                            NativeMethods.ExtractIconExW(iconstringArray[0].Trim('"'), nIconIndex, ref phiconLarge, ref phiconSmall, 1);
                            if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
                            if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
                        }
                    }
                }
            }
            //----以下是Pinvoke生成代码----           [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
            public struct HICON__
            {            /// int   
                public int unused;
            }        public partial class NativeMethods
            {            /// Return Type: UINT->unsigned int   
                ///lpszFile: LPCWSTR->WCHAR*   
                ///nIconIndex: int   
                ///phiconLarge: HICON*   
                ///phiconSmall: HICON*   
                ///nIcons: UINT->unsigned int   
                [System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
                public static extern uint ExtractIconExW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref System.IntPtr phiconLarge, ref System.IntPtr phiconSmall, uint nIcons);        }网上找的,在哪看到的忘记了,主要就是读注册表
      

  8.   

    这个能取到ico,但
    是有2个问题 我用ico制作工具打开我用ico.save出来的文件 ,发现
    1.偏色严重
    2.只有一个大小,但是ico文件本身是可以有多个大小的啊
      

  9.   

    其他的问题现在都解决了 ,现在就是怎么取 ico所有的 大小的图标ExtractIconExW 只能取2种大小的图标,其他大小的图标怎么取呢?
      

  10.   

    [DllImport("shell32", EntryPoint = "SHExtractIconsW", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern int ExtractIcons
            (
                string filename,
                int index,
                int cx,
                int cy,
                IntPtr[] icons,
                int[] iconIds,
                int numberOfIcons,
                int flags
            );用这个方法试试参考: http://social.msdn.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/7c6dfe29-cdaf-41e1-920a-4d40d8b37fb5/
      

  11.   

    通过读注册表,取得文件的关联exe,.调用ExtractIcons 获取所有图标,大概思路就是这样,
    好不好用没试过,自己测试下