Example #1
0
        public static System.Drawing.Icon SmallIcon(string fileName)
        {
            if (MonoHelper.IsUnix)
            {
                return(System.Drawing.Icon.ExtractAssociatedIcon(fileName));
            }

            SHFILEINFO shinfo = new SHFILEINFO();

            if (NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON) == IntPtr.Zero)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
            System.Drawing.Icon tmp = System.Drawing.Icon.FromHandle(shinfo.hIcon);

            try
            {
                System.Drawing.Icon res = (System.Drawing.Icon)tmp.Clone();
                tmp.Dispose();
                NativeMethods.DestroyIcon(shinfo.hIcon);
                return(res);
            }
            catch
            {}
            return(null);
        }