Example #1
0
        private IShellItem GetParentShellItem()
        {
            IShellItem parent = null;

            try
            {
                if (_shellItem?.GetParent(out parent) != NativeMethods.S_OK)
                {
                    parent = null;
                }
            }
            catch (Exception e)
            {
                ShellLogger.Error($"ShellItem: Unable to get parent shell item: {e.Message}");

                // Fall back to the root shell item via empty string
                try
                {
                    parent = GetShellItem(string.Empty);
                }
                catch (Exception exception)
                {
                    ShellLogger.Error($"ShellItem: Unable to get fallback parent shell item: {exception.Message}");
                }
            }

            return(parent);
        }
Example #2
0
        private static IShellFolder CreateShellFolder(IShellItem shellItem)
        {
            IShellItem parentShellItem;

            shellItem.GetParent(out parentShellItem);

            var result = parentShellItem.BindToHandler(IntPtr.Zero, BHID.SFObject, typeof(IShellFolder).GUID);

            return((IShellFolder)Marshal.GetTypedObjectForIUnknown(result, typeof(IShellFolder)));
        }
Example #3
0
        //  pidl array Initialize retry
        public static IntPtr[] GetFolderPidlRetry(string path, ref IShellFolder folder, ref IShellItem parent,
                                                  IntPtr?itemPidl = null)
        {
            string path2 = null;
            IntPtr pidl  = IntPtr.Zero;

            if (itemPidl != null)
            {
                pidl = itemPidl.Value;
            }
            else
            {
                var item = ShPidlSystem.FromPath(path);
                pidl = item.Handle.pidl;
            }

            IShellItem parentInfo = null;

            folder = null;
            IntPtr[] pidls     = EmptyPidls;
            String   pathCheck = null;

            try
            {
                // if (RunningVista)
                IShellItem i = ILShell32.SHCreateItemFromParsingName(path, IntPtr.Zero,
                                                                     typeof(IShellItem).GUID);
                i.GetParent(out parentInfo);

                StringBuilder path2Ref = new StringBuilder(FileSystem.MAX_PATH);
                if (Shell32.SHGetPathFromIDList(pidl, path2Ref) > 0) // == WinError.S_OK)
                {
                    path2 = path2Ref.ToString();
                }
                //return Marshal.StringToHGlobalUni(result.ToString());

                folder = GetIShellFolderCom <IShellFolder>(i);
                IntPtr relativePidl = ILShell32.ILFindLastID(pidl);
                pidls = new IntPtr[] { relativePidl };

                StringBuilder resultStr = new StringBuilder(FileSystem.MAX_PATH);
                if (!ILShell32.SHGetPathFromIDList(relativePidl, resultStr))
                {
                    pathCheck = resultStr.ToString();
                }

                if (relativePidl != IntPtr.Zero)
                {
                    parent = parentInfo;
                }
            }
            catch (Exception)
            {
                // Free the PIDL, reset the result.
                // Marshal.FreeCoTaskMem(relativePidl);
                // throw new InvalidOperationException("Failed to initialise child.", exception);
                folder = null;
                pidls  = EmptyPidls;
            }

            return(pidls);
        }
Example #4
0
        private void Init(ShellItemKnownInfo knownInfo)
        {
            _loadedAttrs     = knownInfo.LoadedAttributes;
            _attrs           = knownInfo.Attributes;
            _parentParsePath = knownInfo.ParentParsePath;

            _pidl         = new Lazy <IdList>(() => SHGetIDListFromObject(nativeItem));
            _parentFolder = new Lazy <ShellFolder>(() =>
            {
                var nativeParent = nativeItem.GetParent();
                if (nativeParent == null)
                {
                    return(null);
                }
                return(new ShellFolder(nativeParent, null, new ShellItemKnownInfo()));
            });
            _desktopAbsoluteParsePath = new Lazy <string>(() => nativeItem.GetDisplayName(SIGDN.DesktopAbsoluteParsing));
            _parseName = new Lazy <string>(() => nativeItem.GetDisplayName(SIGDN.ParentRelativeParsing));
            _parsePath = new Lazy <string>(() =>
            {
                if (Pidl.Parts.Count == 0) // The actual desktop
                {
                    return("");
                }
                try
                {
                    if (_parentParsePath != null)
                    {
                        string normParentPath = _parentParsePath.Trim('\\');
                        if (normParentPath != "")
                        {
                            normParentPath += "\\";
                        }
                        return(normParentPath + ParseName);
                    }
                    else
                    {
                        return(DesktopAbsoluteParsePath);
                    }
                }
                catch (ArgumentException)
                {
                    IdList childPidl = new IdList(Pidl.Parts[Pidl.Parts.Count - 1]);
                    return(GetParentFolder().NativeFolder.GetDisplayNameOf(childPidl, SHGDNF.ForParsing));
                }
            });
            _fullParsePath = new Lazy <string>(() =>
            {
                if (GetParentFolder() == null)
                {
                    return("");
                }
                string parentParsePath = GetParentFolder().FullParsePath.TrimEnd('\\');
                if (parentParsePath != "")
                {
                    parentParsePath += "\\";
                }
                return(parentParsePath + nativeItem.GetDisplayName(SIGDN.ParentRelativeParsing));
            });

            /*_displayName = new Lazy<string>(() => GetParentFolder().NativeFolder.GetDisplayNameOf(
             *  new IdList(Pidl.Parts[Pidl.Parts.Count - 1]),
             *  SHGDNF.InFolder | SHGDNF.Normal));*/
            _displayName = new Lazy <string>(() => nativeItem.GetDisplayName(SIGDN.NormalDisplay));
        }