Exemplo n.º 1
0
        /// <summary>
        /// 获取扩展名信息
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static string GetTypeInfo(string ext)
        {
            Shfileinfo shinfo = new Shfileinfo();

            Win32.SHGetFileInfo(ext, Win32.FileAttributeNormal, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.ShgfiTypename | Win32.ShgfiUsefileattributes);
            return(shinfo.szTypeName);
        }
Exemplo n.º 2
0
        private static string TrueName(string path)
        {
            var info = new Shfileinfo();

            Shell32.SHGetFileInfo(path, 0, ref info, (uint)Marshal.SizeOf(info), 0x000000200);
            return(info.szDisplayName);
        }
Exemplo n.º 3
0
 public static extern IntPtr SHGetFileInfo(
     String pszPath,
     UInt32 dwFileAttributes,
     ref Shfileinfo psfi,
     UInt32 cbFileInfo,
     UInt32 uFlags
     );
Exemplo n.º 4
0
 public static extern IntPtr SHGetFileInfo(
     string pszPath,
     uint dwFileAttributes,
     ref Shfileinfo psfi,
     uint cbFileInfo,
     uint uFlags
     );
Exemplo n.º 5
0
        /// <summary>
        ///   Gets the large icon from the win32 api invocation
        /// </summary>
        /// <param name = "fileName">The file name to lookup ( extension )</param>
        /// <returns></returns>
        public static Icon GetLargeIcon(string fileName)
        {
            var shinfo    = new Shfileinfo();
            var hImgLarge = Win32.SHGetFileInfo(fileName, 0, ref shinfo, ( uint )Marshal.SizeOf(shinfo), Win32.ShgfiIcon | Win32.ShgfiLargeicon | Win32.UseFileAtrributes);
            var icon      = ( Icon )Icon.FromHandle(shinfo.hIcon).Clone();

            Win32.DestroyIcon(shinfo.hIcon);
            return(icon);
        }
Exemplo n.º 6
0
        /// <summary>
        ///   Gets the small icon from the win32 api invocation
        /// </summary>
        /// <param name = "fileName">The file name to lookup ( extension )</param>
        /// <returns></returns>
        public static Icon GetSmallIcon(string fileName)
        {
            var shinfo    = new Shfileinfo();
            var hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, ( uint )Marshal.SizeOf(shinfo), Win32.ShgfiIcon | Win32.ShgfiSmallicon | Win32.UseFileAtrributes);
            //The icon is returned in the hIcon member of the shinfo struct
            var icon = ( Icon )Icon.FromHandle(shinfo.hIcon).Clone();

            Win32.DestroyIcon(shinfo.hIcon);
            return(icon);
        }
Exemplo n.º 7
0
        private static Icon GetIcon(string fileName, uint flags)
        {
            var shinfo = new Shfileinfo();

            Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.ShgfiIcon | flags);

            var icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();

            Win32.DestroyIcon(shinfo.hIcon);
            return(icon);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 获取扩展名的图标
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static Icon GetSmallTypeIcon(string ext)
        {
            Shfileinfo shinfo = new Shfileinfo();

            Win32.SHGetFileInfo(ext, Win32.FileAttributeNormal, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.ShgfiIcon | Win32.ShgfiSmallicon | Win32.ShgfiUsefileattributes);
            if (shinfo.hIcon.ToInt32() == 0)
            {
                return(null);
            }
            Icon shellIcon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone();

            Win32.DestroyIcon(shinfo.hIcon);
            return(shellIcon);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 获取文件的大图标
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Icon GetLargeIcon(string fileName)
        {
            Shfileinfo shinfo = new Shfileinfo();

            Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.ShgfiIcon | Win32.ShgfiLargeicon);
            if (shinfo.hIcon.ToInt32() == 0)
            {
                return(null);
            }
            Icon shellIcon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone();

            Win32.DestroyIcon(shinfo.hIcon);
            return(shellIcon);
        }
Exemplo n.º 10
0
        public static ImageSource GetShellIcon(string filePath)
        {
            ImageSource shellIcon = null;

            try
            {
                Shfileinfo shinfo = new Shfileinfo();

                /*
                 * private const uint SHGFI_ICON = 0x100;
                 * private const uint SHGFI_LARGEICON = 0x0;
                 * private const uint SHGFI_SMALLICON = 0x000000001;
                 */
                SHGetFileInfo(
                    filePath,
                    0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                    0x100 | 0x0);

                using (System.Drawing.Icon i = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                {
                    //Convert icon to a Bitmap source
                    shellIcon = Imaging.CreateBitmapSourceFromHIcon(
                        i.Handle,
                        new Int32Rect(0, 0, i.Width, i.Height),
                        BitmapSizeOptions.FromEmptyOptions());
                }
            }
            catch
            {
                try
                {
                    System.Drawing.Icon fileIcon = System.Drawing.Icon.ExtractAssociatedIcon(filePath);
                    if (fileIcon != null)
                    {
                        using (Bitmap iconBitmap = fileIcon.ToBitmap())
                        {
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
                            iconBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            shellIcon = BitmapFrame.Create(ms);
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            return(shellIcon);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns the index of the icon for the specified file
        /// </summary>
        /// <param name="fileName">Filename to get icon for</param>
        /// <param name="forceLoadFromDisk">If True, then hit the disk to get the icon,
        /// otherwise only hit the disk if no cached icon is available.</param>
        /// <param name="iconState">Flags specifying the state of the icon
        /// returned.</param>
        /// <returns>Index of the icon</returns>
        public int IconIndex(
            string fileName,
            bool forceLoadFromDisk,
            ShellIconStateConstants iconState
            )
        {
            var dwFlags = ShGetFileInfoConstants.ShgfiSysiconindex;
            var dwAttr  = 0;

            if (_size == SysImageListSize.SmallIcons)
            {
                dwFlags |= ShGetFileInfoConstants.ShgfiSmallicon;
            }

            // We can choose whether to access the disk or not. If you don't
            // hit the disk, you may get the wrong icon if the icon is
            // not cached. Also only works for files.
            if (!forceLoadFromDisk)
            {
                dwFlags |= ShGetFileInfoConstants.ShgfiUsefileattributes;
                dwAttr   = FileAttributeNormal;
            }
            else
            {
                dwAttr = 0;
            }

            // sFileSpec can be any file. You can specify a
            // file that does not exist and still get the
            // icon, for example sFileSpec = "C:\PANTS.DOC"
            var shfi     = new Shfileinfo();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal   = SHGetFileInfo(
                fileName, dwAttr, ref shfi, shfiSize,
                ((uint)(dwFlags) | (uint)iconState));

            if (retVal.Equals(IntPtr.Zero))
            {
                System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
                return(0);
            }
            return(shfi.iIcon);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Creates the SystemImageList
        /// </summary>
        private void Create()
        {
            // forget last image list if any:
            _hIml = IntPtr.Zero;

            if (IsXpOrAbove())
            {
                // Get the System IImageList object from the Shell:
                var iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                var ret          = SHGetImageList(
                    (int)_size,
                    ref iidImageList,
                    ref _iImageList
                    );
                // the image list handle is the IUnknown pointer, but
                // using Marshal.GetIUnknownForObject doesn't return
                // the right value.  It really doesn't hurt to make
                // a second call to get the handle:
                SHGetImageListHandle((int)_size, ref iidImageList, ref _hIml);
            }
            else
            {
                // Prepare flags:
                var dwFlags = ShGetFileInfoConstants.ShgfiUsefileattributes | ShGetFileInfoConstants.ShgfiSysiconindex;
                if (_size == SysImageListSize.SmallIcons)
                {
                    dwFlags |= ShGetFileInfoConstants.ShgfiSmallicon;
                }
                // Get image list
                var shfi     = new Shfileinfo();
                var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

                // Call SHGetFileInfo to get the image list handle
                // using an arbitrary file:
                _hIml = SHGetFileInfo(
                    ".txt",
                    FileAttributeNormal,
                    ref shfi,
                    shfiSize,
                    (uint)dwFlags);
                System.Diagnostics.Debug.Assert((_hIml != IntPtr.Zero), "Failed to create Image List");
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Получить icon, ассоциированную с файлом
        /// </summary>
        /// <param name="fileName">Полный путь к файлу</param>
        /// <returns>Возвращает Icon файла</returns>
        public static Icon GetIcon(string fileName)
        {
            var shfi = new Shfileinfo();
            var res  = SHGetFileInfo(fileName,
                                     (uint)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
                                     ref shfi,
                                     (uint)Marshal.SizeOf(shfi),
                                     (uint)(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_USEFILEATTRIBUTES | FileInfoFlags.SHGFI_SMALLICON));

            if (res == IntPtr.Zero || shfi.hIcon == IntPtr.Zero)
            {
                return(null);
            }

            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

            DestroyIcon(shfi.hIcon);
            return(icon);
        }
        public static Icon IconFromExtensionShell(string _extension_, SystemIconSize _size_)
        {
            //add '.' if nessesry
            if (_extension_[0] != '.')
            {
                _extension_ = '.' + _extension_;
            }

            //temp struct for getting file shell info
            Shfileinfo fileInfo = new Shfileinfo();

            SHGetFileInfo(
                _extension_,
                0,
                out fileInfo,
                Marshal.SizeOf(fileInfo),
                FileInfoFlags.ShgfiIcon | FileInfoFlags.ShgfiUsefileattributes | (FileInfoFlags)_size_);

            return(Icon.FromHandle(fileInfo.hIcon));
        }
Exemplo n.º 15
0
        /// <summary>
        ///     依据文件名读取图标,若指定文件不存在,则返回空值。
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Icon GetIconByFileName(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }
            if (!File.Exists(fileName))
            {
                return(null);
            }
            var shInfo = new Shfileinfo();

            //Use this to get the small Icon
            Win32.SHGetFileInfo(fileName, 0, ref shInfo, (uint)Marshal.SizeOf(shInfo),
                                Win32.ShgfiIcon | Win32.ShgfiSmallicon);
            //The icon is returned in the hIcon member of the shinfo struct
            var myIcon = Icon.FromHandle(shInfo.hIcon);

            return(myIcon);
        }
Exemplo n.º 16
0
        private bool ShFileInfoValidation(string file)
        {
            IntPtr     ptr  = IntPtr.Zero;
            Shfileinfo info = new Shfileinfo();

            ptr = SHGetFileInfo(file, 128, ref info, (uint)Marshal.SizeOf(info), Exetype);

            int intParam = ptr.ToInt32();
            int loWord   = intParam & 0xffff;
            int hiWord   = intParam >> 16;

            FileType type = FileType.Invalid;

            if (intParam == 0)
            {
                return(IsValidDll(file));
            }

            switch (hiWord)
            {
            case 0x0000 when loWord == 0x5a4d:
                type = FileType.Dos;
                break;

            case 0x0000 when loWord == 0x4550:
                type = FileType.Win32Console;
                break;

            default:
                if ((hiWord != 0x0000) && (loWord == 0x454E || loWord == 0x4550 || loWord == 0x454C))
                {
                    type = FileType.WindowsExe;
                }
                break;
            }

            return(type != FileType.Invalid);
        }
Exemplo n.º 17
0
        /// <summary>
        ///  Gets the information for the specified
        ///  file name and flags.
        /// </summary>
        public void GetInfo()
        {
            _fileIcon    = null;
            _typeName    = "";
            _displayName = "";

            Shfileinfo shfi     = new Shfileinfo();
            uint       shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

            int ret = SHGetFileInfo(
                _fileName, 0, ref shfi, shfiSize, (uint)(_flags));

            if (ret != 0)
            {
                if (shfi.hIcon != IntPtr.Zero)
                {
                    _fileIcon = System.Drawing.Icon.FromHandle(shfi.hIcon);
                    // Now owned by the GDI+ object
                    //DestroyIcon(shfi.hIcon);
                }
                _typeName    = shfi.szTypeName;
                _displayName = shfi.szDisplayName;
            }
            else
            {
                int err = GetLastError();
                Console.WriteLine("Error {0}", err);
                string txtS = new string('\0', 256);
                int    len  = FormatMessage(
                    FormatMessageFromSystem | FormatMessageIgnoreInserts,
                    IntPtr.Zero, err, 0, txtS, 256, 0);
                Console.WriteLine("Len {0} text {1}", len, txtS);

                // throw exception
            }
        }
Exemplo n.º 18
0
 public static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref Shfileinfo psfi,
     int cbFileInfo, ShGetFileInfoFlags flags);
Exemplo n.º 19
0
 public static extern IntPtr SHGetFileInfo(
   IntPtr pszPath,
   uint dwFileAttributes,
   out  Shfileinfo psfi,
   uint cbfileInfo,
   uint uFlags);
 extern static IntPtr SHGetFileInfo(
     string _pszPath_,
     int _dwFileAttributes_,
     out Shfileinfo _psfi_,
     int _cbFileInfo_,
     FileInfoFlags _uFlags_);
Exemplo n.º 21
0
 public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref Shfileinfo psfi,
     uint cbSizeFileInfo, uint uFlags);
Exemplo n.º 22
0
 /// <summary>
 ///     依据文件名读取图标,若指定文件不存在,则返回空值。
 /// </summary>
 /// <param name="fileName"></param>
 /// <returns></returns>
 public static Icon GetIconByFileName(string fileName)
 {
     if (string.IsNullOrEmpty(fileName)) return null;
     if (!File.Exists(fileName)) return null;
     var shInfo = new Shfileinfo();
     //Use this to get the small Icon
     Win32.SHGetFileInfo(fileName, 0, ref shInfo, (uint) Marshal.SizeOf(shInfo),
         Win32.ShgfiIcon | Win32.ShgfiSmallicon);
     //The icon is returned in the hIcon member of the shinfo struct
     var myIcon = Icon.FromHandle(shInfo.hIcon);
     return myIcon;
 }
Exemplo n.º 23
0
 private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, out Shfileinfo psfi,
                                         uint cbFileInfo, uint flags);
Exemplo n.º 24
0
 public static extern int SHGetFileInfo(
     string pszPath,
     FileAttribute dwFileAttributes,
     out Shfileinfo psfi,
     uint cbfileInfo,
     Shgfi uFlags);
Exemplo n.º 25
0
 [DllImport("shell32.dll")] internal static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref Shfileinfo psfi, uint cbSizeFileInfo, uint uFlags);