コード例 #1
0
        public static Icon FromExtension(string fileOrExtension, SystemIconSize size)
        {
            if (fileOrExtension == null)
            {
                throw new ArgumentNullException(nameof(fileOrExtension), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                return(SystemIcons.WinLogo);
            }

            if (!Path.HasExtension(fileOrExtension))
            {
                fileOrExtension = Path.GetFileName(fileOrExtension) == fileOrExtension ? '.' + fileOrExtension : ".";
            }

            IntPtr handle = Shell32.GetFileIconHandle(fileOrExtension, size);

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException(PublicResources.ArgumentInvalidString, nameof(fileOrExtension));
            }

            return(Icon.FromHandle(handle).ToManagedIcon());
        }
コード例 #2
0
        public static Icon[] FromFile(string fileName, SystemIconSize size)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName), PublicResources.ArgumentNull);
            }
            if (!Enum <SystemIconSize> .IsDefined(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), PublicResources.EnumOutOfRangeWithValues(size));
            }
            if (!OSUtils.IsWindows)
            {
                Reflector.EmptyArray <Icon>();
            }

            IntPtr[][] handles = Shell32.ExtractIconHandles(fileName, size);
            Icon[]     result  = new Icon[handles.Length];

            for (int i = 0; i < handles.Length; i++)
            {
                result[i] = Icon.FromHandle(handles[i][0]).ToManagedIcon();
            }

            return(result);
        }