Пример #1
0
        /// <summary>
        /// load computer display name and icon
        /// </summary>
        /// <param name="displayName"></param>
        /// <param name="computerIcon"></param>
        public static void LoadComputer(out string displayName, out ImageSource computerIcon)
        {
            try
            {
                IntPtr ptrIDL = IntPtr.Zero;
                var    result = ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, ShellAPI.CSIDL.CSIDL_DRIVES, ref ptrIDL);
                if (result != 0)
                {
                    Marshal.ThrowExceptionForHR(result);
                }

                ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO();
                ShellAPI.SHGFI      vFlags    =
                    ShellAPI.SHGFI.SHGFI_SMALLICON |
                    ShellAPI.SHGFI.SHGFI_ICON |
                    ShellAPI.SHGFI.SHGFI_PIDL |
                    ShellAPI.SHGFI.SHGFI_DISPLAYNAME |
                    ShellAPI.SHGFI.SHGFI_TYPENAME |
                    ShellAPI.SHGFI.SHGFI_ADDOVERLAYS;
                ShellAPI.SHGetFileInfo(ptrIDL, 0, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags);
                displayName  = shellInfo.szDisplayName;
                computerIcon = GetImageSourceFromIcon(shellInfo.hIcon);
                if (shellInfo.hIcon != IntPtr.Zero)
                {
                    User32API.DestroyIcon(shellInfo.hIcon);
                    shellInfo.hIcon = IntPtr.Zero;
                }
            }
            catch (Exception ex)
            {
                displayName  = string.Empty;
                computerIcon = null;
                LogHelper.Debug("Application Exception:", ex);
            }
        }
Пример #2
0
        public static ImageSource LoadDriveIcon(string path)
        {
            if (path == null)
            {
                return(null);
            }
            //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP.
            //change ftp path '/' to '\' for XP.
            path = path.Replace('/', '\\');
            ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGFI      vFlags    =
                ShellAPI.SHGFI.SHGFI_LARGEICON |
                ShellAPI.SHGFI.SHGFI_ICON |
                ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES |
                ShellAPI.SHGFI.SHGFI_DISPLAYNAME;

            ShellAPI.SHGetFileInfo(path, (uint)FileAttributes.Directory, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags);
            var ico = GetImageSourceFromIcon(shellInfo.hIcon);

            if (shellInfo.hIcon != IntPtr.Zero)
            {
                User32API.DestroyIcon(shellInfo.hIcon);
                shellInfo.hIcon = IntPtr.Zero;
            }
            return(ico);
        }
Пример #3
0
        /// <summary>
        /// This method uses SHGetFileInfo to check whether a path to a directory exists.
        /// </summary>
        /// <param name="path">The path to check</param>
        /// <returns>true if it exists, false otherwise</returns>
        private bool PathExists(string path)
        {
            string realPath = ConvertPath(path);

            if (string.IsNullOrEmpty(realPath))
            {
                return(false);
            }
            else if (string.Compare(path, "desktop", true) == 0)
            {
                return(true);
            }

            string[] pathParts = realPath.Split('\\');

            for (int i = 0; i < pathParts.Length; i++)
            {
                bool found = false;
                if (ShellBrowser.DesktopItem.SubFolders.Contains(pathParts[i]))
                {
                    pathParts[i] = ShellItem.GetRealPath(
                        ShellBrowser.DesktopItem.SubFolders[pathParts[i]]);

                    found = true;
                }
                else
                {
                    ShellItem myComp =
                        ShellBrowser.DesktopItem.SubFolders[ShellBrowser.MyComputerName];

                    if (myComp.SubFolders.Contains(pathParts[i]))
                    {
                        pathParts[i] = ShellItem.GetRealPath(
                            myComp.SubFolders[pathParts[i]]);

                        found = true;
                    }
                }

                if (!found)
                {
                    break;
                }
            }

            realPath = string.Join("\\", pathParts);

            if (realPath.EndsWith(":"))
            {
                realPath += "\\";
            }

            ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO();
            IntPtr ptr    = ShellAPI.SHGetFileInfo(realPath, 0, ref info, ShellAPI.cbFileInfo, ShellAPI.SHGFI.DISPLAYNAME);
            bool   exists = (ptr != IntPtr.Zero);

            Marshal.FreeCoTaskMem(ptr);
            return(exists);
        }
Пример #4
0
        private void UpdateNodeImage(TreeNode node)
        {
            int imageIndex = -1;

            if (node.Tag != null)
            {
                ProjectItem item = (ProjectItem)node.Tag;
                if (item is Folder || item is ProjectDocumentItem || item is RootItem)
                {
                    ShellAPI.SHFILEINFO shInfo = new ShellAPI.SHFILEINFO();
                    ShellAPI.SHGFI      attrs  = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_SYSICONINDEX;
                    if (node.IsExpanded)
                    {
                        attrs |= ShellAPI.SHGFI.SHGFI_OPENICON;
                    }

                    IntPtr handle = ShellAPI.SHGetFileInfo(
                        System.Windows.Forms.Application.StartupPath,
                        ShellAPI.FILE_ATTRIBUTE_NORMAL,
                        out shInfo, (uint)Marshal.SizeOf(shInfo),
                        attrs);

                    imageIndex = shInfo.iIcon;
                }

                else if (item is DocumentItem)
                {
                    string fileName            = ((DocumentItem)item).AbsoluteFileName;
                    ShellAPI.SHFILEINFO shInfo = new ShellAPI.SHFILEINFO();
                    ShellAPI.SHGFI      attrs  = ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_SYSICONINDEX;
                    IntPtr handle = ShellAPI.SHGetFileInfo(
                        fileName,
                        ShellAPI.FILE_ATTRIBUTE_NORMAL,
                        out shInfo, (uint)Marshal.SizeOf(shInfo),
                        attrs);

                    imageIndex = shInfo.iIcon;
                }

                else
                {
                    ShellAPI.SHFILEINFO shInfo = new ShellAPI.SHFILEINFO();
                    ShellAPI.SHGFI      attrs  = ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_SYSICONINDEX;
                    IntPtr handle = ShellAPI.SHGetFileInfo(
                        "",
                        ShellAPI.FILE_ATTRIBUTE_NORMAL,
                        out shInfo, (uint)Marshal.SizeOf(shInfo),
                        attrs);
                    imageIndex = shInfo.iIcon;
                }

                node.Text = item.Label;
            }
            node.ImageIndex         = imageIndex;
            node.SelectedImageIndex = imageIndex;
        }
Пример #5
0
        public static Bitmap GetFileIcon(string name)
        {
            ShellDll.ShellAPI.SHFILEINFO shinfo = new ShellAPI.SHFILEINFO();

            ShellAPI.SHGetFileInfo(name, ShellAPI.FILE_ATTRIBUTE.NORMAL,
                                   ref shinfo, (int)Marshal.SizeOf(shinfo),
                                   ShellAPI.SHGFI.ICON | ShellAPI.SHGFI.USEFILEATTRIBUTES);

            return(Icon.FromHandle(shinfo.hIcon).ToBitmap());
        }
Пример #6
0
        public void BuildDriveList()
        {
            base.Items.Clear();

            ShellAPI.SHFILEINFO shInfo    = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGFI      dwAttribs =
                ShellAPI.SHGFI.SHGFI_ICON |
                ShellAPI.SHGFI.SHGFI_SMALLICON |
                ShellAPI.SHGFI.SHGFI_SYSICONINDEX |
                ShellAPI.SHGFI.SHGFI_DISPLAYNAME;

            ListDictionary _iconDict = new ListDictionary();

            foreach (string drive in System.IO.Directory.GetLogicalDrives())
            {
                IntPtr m_pHandle = ShellAPI.SHGetFileInfo(drive, ShellAPI.FILE_ATTRIBUTE_NORMAL, ref shInfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shInfo), dwAttribs);

                if (m_pHandle.Equals(IntPtr.Zero) == false)
                {
                    int idxIcon = 0;
                    if (_iconDict.Contains(shInfo.iIcon) == false)
                    {
                        base.ImageList.Images.Add(System.Drawing.Icon.FromHandle(shInfo.hIcon).Clone() as System.Drawing.Icon);

                        User32API.DestroyIcon(shInfo.hIcon);

                        _iconDict.Add(shInfo.iIcon, _iconDict.Count);
                        idxIcon = _iconDict.Count - 1;
                    }
                    else
                    {
                        idxIcon = Convert.ToInt32(_iconDict[shInfo.iIcon]);
                    }

                    try
                    {
                        DriveInfo drv_info = new DriveInfo(shInfo.szDisplayName.Substring(shInfo.szDisplayName.IndexOf("(") + 1, 1));
                        if (drv_info.DriveType == DriveType.CDRom)
                        {
                            ImageComboItem item = new ImageComboItem(shInfo.szDisplayName, idxIcon, false);
                            item.ItemValue = drive;
                            base.Items.Add(item);
                        }
                    }
                    catch
                    {
                    }
                }
            }

            if (base.Items.Count != 0)
            {
                base.SelectedIndex = 0;
            }
        }
Пример #7
0
        static ShellImageList()
        {
            imageTable = new Hashtable();

            ShellAPI.SHGFI      flag      = ShellAPI.SHGFI.USEFILEATTRIBUTES | ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.SMALLICON;
            ShellAPI.SHFILEINFO shfiSmall = new ShellAPI.SHFILEINFO();
            smallImageListHandle = ShellAPI.SHGetFileInfo(".txt", ShellAPI.FILE_ATTRIBUTE.NORMAL, ref shfiSmall, ShellAPI.cbFileInfo, flag);

            flag = ShellAPI.SHGFI.USEFILEATTRIBUTES | ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.LARGEICON;
            ShellAPI.SHFILEINFO shfiLarge = new ShellAPI.SHFILEINFO();
            largeImageListHandle = ShellAPI.SHGetFileInfo(".txt", ShellAPI.FILE_ATTRIBUTE.NORMAL, ref shfiLarge, ShellAPI.cbFileInfo, flag);
        }
Пример #8
0
        /// <summary>
        /// Load file  icon, file type from shell
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="attributes">The attr.</param>
        public static System.Windows.Media.ImageSource LoadFileInfo(string path, out string typeName)
        {
            FileAttributes attributes;

            if (CommonProvider.Directory.Exists(path))
            {
                attributes = FileAttributes.Directory;
            }
            else if (CommonProvider.File.Exists(path))
            {
                attributes = FileAttributes.Normal;
            }
            else
            {
                typeName = string.Empty;
                return(null);
            }

            typeName = string.Empty;
            if (path == null)
            {
                return(null);
            }
            //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP.
            //change ftp path '/' to '\' for XP.
            path = path.Replace('/', '\\');
            ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGFI      vFlags    =
                ShellAPI.SHGFI.SHGFI_SMALLICON |
                ShellAPI.SHGFI.SHGFI_ICON |
                ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES |
                ShellAPI.SHGFI.SHGFI_TYPENAME;

            ShellAPI.SHGetFileInfo(path, (uint)attributes, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags);
            var ico = GetImageSourceFromIcon(shellInfo.hIcon);

            //displayName = shellInfo.szDisplayName;
            if (shellInfo.hIcon != IntPtr.Zero)
            {
                User32API.DestroyIcon(shellInfo.hIcon);
                shellInfo.hIcon = IntPtr.Zero;
            }
            typeName = shellInfo.szTypeName;
            if (ico == null && attributes == FileAttributes.Directory)
            {
                ico = DefaultFolderImage;
            }
            return(ico);
        }
Пример #9
0
        protected static ShellDll.ShellAPI.SFGAO shGetFileAttribute(PIDL pidl, ShellDll.ShellAPI.SFGAO lookup)
        {
            ShellAPI.SHFILEINFO shfi = new ShellAPI.SHFILEINFO();
            shfi.dwAttributes = ShellAPI.SFGAO.READONLY | ShellAPI.SFGAO.HIDDEN | ShellAPI.SFGAO.BROWSABLE |
                                ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.HASSUBFOLDER;
            ShellAPI.SHGFI          dwFlag = ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.ATTRIBUTES | ShellAPI.SHGFI.ATTR_SPECIFIED | ShellAPI.SHGFI.USEFILEATTRIBUTES;
            ShellAPI.FILE_ATTRIBUTE dwAttr = 0;
            int    cbFileInfo = Marshal.SizeOf(shfi.GetType());
            IntPtr retPtr     = ShellAPI.SHGetFileInfo(pidl.Ptr, dwAttr, ref shfi, cbFileInfo, dwFlag);

            if (retPtr.ToInt32() != ShellAPI.S_OK && retPtr.ToInt32() != 1)
            {
                Marshal.ThrowExceptionForHR(retPtr.ToInt32());
            }
            return(shfi.dwAttributes);
        }
Пример #10
0
        /// <summary>
        /// Load file icon
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="attributes">The attr.</param>
        public static ImageSource LoadFileInfo(string path, FileAttributes attributes)
        {
            if (path == null)
            {
                return(null);
            }
            if (attributes == FileAttributes.Normal)
            {
                if (!CommonProvider.File.Exists(path))
                {
                    path = Path.GetFileName(path);
                }
            }

            //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP.
            //change ftp path '/' to '\' for XP.
            path = path.Replace('/', '\\');
            ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGFI      vFlags    =
                ShellAPI.SHGFI.SHGFI_SMALLICON |
                ShellAPI.SHGFI.SHGFI_ICON |
                ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES;

            ShellAPI.SHGetFileInfo(path, (uint)attributes, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags);
            var ico = GetImageSourceFromIcon(shellInfo.hIcon);

            if (shellInfo.hIcon != IntPtr.Zero)
            {
                User32API.DestroyIcon(shellInfo.hIcon);
                shellInfo.hIcon = IntPtr.Zero;
            }

            if (ico == null && attributes == FileAttributes.Directory)
            {
                ico = DefaultFolderImage;
            }
            return(ico);
        }
Пример #11
0
        private void InitVars()
        {
            IntPtr tempPidl;

            ShellAPI.SHFILEINFO info;

            //My Computer
            info     = new ShellAPI.SHFILEINFO();
            tempPidl = IntPtr.Zero;
            ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, ShellAPI.CSIDL.DRIVES, out tempPidl);

            ShellAPI.SHGetFileInfo(tempPidl, 0, ref info, ShellAPI.cbFileInfo,
                                   ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.DISPLAYNAME | ShellAPI.SHGFI.TYPENAME);

            sysfolderName = info.szTypeName;
            mycompName    = info.szDisplayName;
            Marshal.FreeCoTaskMem(tempPidl);
            //

            //Dekstop
            tempPidl    = IntPtr.Zero;
            desktopItem = new ShellItem(this, ShellAPI.CSIDL.DESKTOP);
            desktopItem.Expand(true, true, IntPtr.Zero);
            //
            myComputerItem = desktopItem.SubFolders[MyComputerName];
            //My Documents
            uint pchEaten = 0;

            ShellAPI.SFGAO pdwAttributes = 0;
            desktopItem.ShellFolder.ParseDisplayName(
                IntPtr.Zero,
                IntPtr.Zero,
                "::{450d8fba-ad25-11d0-98a8-0800361b1103}",
                ref pchEaten,
                out tempPidl,
                ref pdwAttributes);

            info = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGetFileInfo(tempPidl, 0, ref info, ShellAPI.cbFileInfo,
                                   ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.DISPLAYNAME);

            mydocsName = info.szDisplayName;
            Marshal.FreeCoTaskMem(tempPidl);

            StringBuilder path = new StringBuilder(ShellAPI.MAX_PATH);

            ShellAPI.SHGetFolderPath(
                IntPtr.Zero, ShellAPI.CSIDL.PERSONAL,
                IntPtr.Zero, ShellAPI.SHGFP.TYPE_CURRENT, path);
            mydocsPath = path.ToString();
            //
            pchEaten      = 0;
            pdwAttributes = 0;
            desktopItem.ShellFolder.ParseDisplayName(
                IntPtr.Zero,
                IntPtr.Zero,
                "::{645FF040-5081-101B-9F08-00AA002F954E}",
                ref pchEaten,
                out tempPidl,
                ref pdwAttributes);

            info = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGetFileInfo(tempPidl, 0, ref info, ShellAPI.cbFileInfo,
                                   ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.DISPLAYNAME);

            recycleBinName = info.szDisplayName;
            Marshal.FreeCoTaskMem(tempPidl);
        }
        /// <summary>
        /// Get type and icon associated with given file/directory
        /// </summary>
        /// 
        public static bool GetFileInfo(string path, FileAttributes attr, ref ImageSource imageSource, ref string type)
        {
            ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO();
            ShellAPI.SHGFI vFlags =
                ShellAPI.SHGFI.SHGFI_SMALLICON |
                ShellAPI.SHGFI.SHGFI_ICON |
                ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES |
                ShellAPI.SHGFI.SHGFI_DISPLAYNAME |
                ShellAPI.SHGFI.SHGFI_TYPENAME;
            ShellAPI.SHGetFileInfo(path, (uint)attr, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags);

            // (1) Type
            type = shellInfo.szTypeName;

            // (2) Image Source.
            imageSource = null;
            if (shellInfo.hIcon != null)
            {
                try
                {
                    MemoryStream memStream = new MemoryStream();
                    ((System.Drawing.Bitmap)System.Drawing.Icon.FromHandle(shellInfo.hIcon).ToBitmap()).
                        Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
                    memStream.Seek(0, SeekOrigin.Begin);
                    PngBitmapDecoder pngBmpDecoder = new PngBitmapDecoder(memStream,
                         BitmapCreateOptions.PreservePixelFormat,
                          BitmapCacheOption.Default);
                    imageSource = pngBmpDecoder.Frames[0];
                    //User32API.DestroyIcon(shellInfo.hIcon);
                }
                catch (Exception exp)
                {
                    //Debug.Assert(false, exp.Message);
                }

                return true;
            }
            return false;
        }
Пример #13
0
        internal static void SetIconIndex(ShellItem item, int index, bool SelectedIcon)
        {
            bool HasOverlay = false; //true if it's an overlay
            int  rVal       = 0;     //The returned Index

            ShellAPI.SHGFI          dwflag = ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.ICON;
            ShellAPI.FILE_ATTRIBUTE dwAttr = 0;
            //build Key into HashTable for this Item
            int Key = index * 256;

            if (item.IsLink)
            {
                Key        = Key | 1;
                dwflag     = dwflag | ShellAPI.SHGFI.LINKOVERLAY;
                HasOverlay = true;
            }
            if (item.IsShared)
            {
                Key        = Key | 2;
                dwflag     = dwflag | ShellAPI.SHGFI.ADDOVERLAYS;
                HasOverlay = true;
            }
            if (SelectedIcon)
            {
                Key        = Key | 4;
                dwflag     = dwflag | ShellAPI.SHGFI.OPENICON;
                HasOverlay = true; //not really an overlay, but handled the same
            }

            if (imageTable.ContainsKey(Key))
            {
                rVal = (int)imageTable[Key];
            }
            else if (!HasOverlay && !item.IsHidden)                          //for non-overlay icons, we already have
            {
                rVal            = (int)System.Math.Floor((double)Key / 256); // the right index -- put in table
                imageTable[Key] = rVal;
            }
            else //don't have iconindex for an overlay, get it.
            {
                if (item.IsFileSystem & !item.IsDisk & !item.IsFolder)
                {
                    dwflag = dwflag | ShellAPI.SHGFI.USEFILEATTRIBUTES;
                    dwAttr = dwAttr | ShellAPI.FILE_ATTRIBUTE.NORMAL;
                }

                PIDL pidlFull = item.PIDLFull;

                ShellAPI.SHFILEINFO shfiSmall = new ShellAPI.SHFILEINFO();
                ShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiSmall, ShellAPI.cbFileInfo, dwflag | ShellAPI.SHGFI.SMALLICON);

                ShellAPI.SHFILEINFO shfiLarge = new ShellAPI.SHFILEINFO();
                ShellAPI.SHGetFileInfo(pidlFull.Ptr, dwAttr, ref shfiLarge, ShellAPI.cbFileInfo, dwflag | ShellAPI.SHGFI.LARGEICON);

                Marshal.FreeCoTaskMem(pidlFull.Ptr);

                lock (imageTable)
                {
                    rVal = ShellAPI.ImageList_ReplaceIcon(smallImageListHandle, -1, shfiSmall.hIcon);
                    ShellAPI.ImageList_ReplaceIcon(largeImageListHandle, -1, shfiLarge.hIcon);
                }

                ShellAPI.DestroyIcon(shfiSmall.hIcon);
                ShellAPI.DestroyIcon(shfiLarge.hIcon);
                imageTable[Key] = rVal;
            }

            if (SelectedIcon)
            {
                item.SelectedImageIndex = rVal;
            }
            else
            {
                item.ImageIndex = rVal;
            }
        }