示例#1
0
 public static extern HResult SHGetFileInfo([In, MarshalAs(LPWStr)] string pszPath, [MarshalAs(U4)] FileAttributes dwFileAttributes, [In, Out] ref SHFILEINFO psfi, [MarshalAs(U4)] uint cbFileInfo, GetFileInfoOptions uFlags);
        /// <summary>
        /// Retrieves information about an object in the file system, such as a file, folder, directory, or drive root and a value that indicates the exe type.
        /// </summary>
        /// <param name="path"><para>A string of maximum length <see cref="MaxPath"/> that contains the path and file name. Both absolute and relative paths are valid.</para>
        /// <para>If the <b>uFlags</b> parameter includes the <see cref="GetFileInfoOptions.PIDL"/> flag, this parameter must be the address of an ITEMIDLIST(PIDL) structure that contains the list of item identifiers that uniquely identifies the file within the Shell's namespace. The PIDL must be a fully qualified PIDL. Relative PIDLs are not allowed.</para>
        /// <para>If the <b>uFlags</b> parameter includes the <see cref="GetFileInfoOptions.UseFileAttributes"/> flag, this parameter does not have to be a valid file name. The function will proceed as if the file exists with the specified name and with the file attributes passed in the <b>dwFileAttributes</b> parameter. This allows you to obtain information about a file type by passing just the extension for <b>pszPath</b> and passing <see cref="FileAttributes.Normal"/> in <b>dwFileAttributes</b>.</para>
        /// <para>This string can use either short (the 8.3 form) or long file names.</para></param>
        /// <param name="fileAttributes">A combination of one or more <see cref="FileAttributes"/> flags. If <b>uFlags</b> does not include the <see cref="GetFileInfoOptions.UseFileAttributes"/> flag, this parameter is ignored.</param>
        /// <param name="options">The flags that specify the file information to retrieve. This parameter can be a combination of the values of the <see cref="GetFileInfoOptions"/> enum.</param>
        /// <param name="exeType">The exe type. In order to this method retrieves the exe type, you need to use the <see cref="GetFileInfoOptions.ExeType"/> flag in the <b>options</b> parameter.</param>
        /// <returns>A <see cref="FileInfo"/> structure that contains the file information.</returns>
        public static FileInfo GetFileInfo(string path, FileAttributes fileAttributes, GetFileInfoOptions options, out int exeType)
        {
            var psfi = new SHFILEINFO();

            HResult hr = SHGetFileInfo(path, fileAttributes, ref psfi, (uint)Marshal.SizeOf(psfi), options);

            if (!CoreErrorHelper.Succeeded(hr))
            {
                Marshal.ThrowExceptionForHR((int)hr);
            }

            exeType = options.HasFlag(GetFileInfoOptions.ExeType) ? (int)hr : 0;

            Icon icon;

            if (psfi.hIcon == IntPtr.Zero)
            {
                icon = null;
            }

            else

            {
                icon = (Icon)Icon.FromHandle(psfi.hIcon).Clone();

                _ = Core.DestroyIcon(psfi.hIcon);
            }

            return(new FileInfo(icon, psfi.iIcon, psfi.dwAttributes, psfi.szDisplayName, psfi.szTypeName));
        }