示例#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());
        }