Exemple #1
0
        private IShellItem GetItem(Uri uri, ResourceFlags flags = 0)
        {
            var rel = baseUri.MakeRelativeUri(uri);

            if (rel.IsAbsoluteUri)
            {
                throw new ArgumentException("URI is not within this subsystem.", "uri");
            }
            if (String.IsNullOrEmpty(rel.OriginalString))
            {
                return(GetDesktop());
            }
            var segments = rel.OriginalString.Split('/').Where(s => s != "." && s != "").Select(s => HttpUtility.UrlDecode(s)).ToList();

            if (segments.Count == 0)
            {
                return(GetDesktop());
            }

            IShellItem item = null;

            foreach (var name in segments)
            {
                if (item == null)
                {
                    try{
                        item = Shell32.SHCreateItemFromParsingName <IShellItem>("shell:" + name, null);
                        continue;
                    }catch (IOException)
                    {
                        try{
                            item = Shell32.SHCreateItemFromParsingName <IShellItem>(name, null);
                            continue;
                        }catch (IOException)
                        {
                            item = GetDesktop();
                        }
                    }
                }
                if ((flags & ResourceFlags.FollowLinks) != 0)
                {
                    var target = GetTargetItem(item) as IShellItem;
                    if (target != null)
                    {
                        item = target;
                    }
                }
                var    psf = item.BindToHandler <IShellFolder>(null, Shell32.BHID_SFObject);
                uint   tmp;
                IntPtr pidl;
                psf.ParseDisplayName(OwnerHwnd, null, name, out tmp, out pidl, 0);
                try{
                    item = Shell32.SHCreateItemWithParent <IShellItem>(IntPtr.Zero, psf, pidl);
                }finally{
                    Marshal.FreeCoTaskMem(pidl);
                }
            }

            return(item);
        }
Exemple #2
0
 internal ShellFolder(IShellItem shellItem, IShellFolder shellFolder, ShellItemKnownInfo knownInfo)
     : base(GetShellItemFromFolder(shellItem, shellFolder), knownInfo)
 {
     if (shellFolder == null)
     {
         shellFolder = (IShellFolder)shellItem.BindToHandler(null, ShellConsts.BHID_SFObject, typeof(IShellFolder).GUID);
     }
     NativeFolder = shellFolder;
 }
 internal ShellFolder(IShellItem shellItem, IShellFolder shellFolder, ShellItemKnownInfo knownInfo)
     : base(GetShellItemFromFolder(shellItem, shellFolder), knownInfo)
 {
     if (shellFolder == null)
     {
         shellFolder = (IShellFolder)shellItem.BindToHandler(null, ShellConsts.BHID_SFObject, typeof(IShellFolder).GUID);
     }
     NativeFolder = shellFolder;
 }
        private static IEnumShellItems GetEnumShellItems(IShellItem shellItem)
        {
            var guidEnumItems = new Guid("94f60519-2850-4924-aa5a-d15e84868039");
            var hr            = shellItem.BindToHandler(IntPtr.Zero, guidEnumItems, typeof(IEnumShellItems).GUID,
                                                        out var pEnumItems);

            ThrowExceptionForHR(hr);
            return(Marshal.GetObjectForIUnknown(pEnumItems) as IEnumShellItems);
        }
        //private static Guid BHID_SFObject = new Guid("3981e224-f559-11d3-8e3a-00c04f6837d5");

        public static T GetIShellFolderCom <T>(IShellItem ptr)
        {
            IntPtr result = ptr.BindToHandler(IntPtr.Zero,
                                              BHID.SFObject,
                                              typeof(T).GUID);

            var ret = (T)Marshal.GetTypedObjectForIUnknown(result,
                                                           typeof(T));

            return(ret);
        }
        // Synopsis:  Creates an IShellItemArray containing the sample files to be used in the CopyMultipleFiles sample
        //
        // Arguments: psiSrc - Source folder item
        //
        // Returns:   S_OK if successful
        private static IShellItemArray CreateShellItemArrayOfSampleFiles(IShellItem psiSrc)
        {
            var psfSampleSrc   = psiSrc.BindToHandler <IShellFolder>(null, BHID.BHID_SFObject.Guid());
            var rgpidlChildren = new PIDL[c_cMaxFilesToCreate];

            try
            {
                for (var i = 0; i < rgpidlChildren.Length; i++)
                {
                    var   szSampleFileName = string.Format("{0}{1}.{2}", c_szSampleFileName, i, c_szSampleFileExt);
                    SFGAO attr             = 0;
                    psfSampleSrc.ParseDisplayName(default, null, szSampleFileName, out _, out rgpidlChildren[i], ref attr);
    public static IEnumerable <IShellItem> Enumerate(this IShellItem si)
    {
        object pesiTemp;

        si.BindToHandler(IntPtr.Zero, BHID_EnumItems, typeof(IEnumShellItems).GUID, out pesiTemp);
        var pesi = (IEnumShellItems)pesiTemp;

        while (true)
        {
            IShellItem item = null;
            int        fetched;
            pesi.Next(1, out item, out fetched);
            if (fetched == 0)
            {
                break;
            }
            yield return(item);
        }
    }
        /// <summary>
        /// Refresh the contents of the directory.
        /// </summary>
        public void Refresh(StandardIcons?iconSize = default)
        {
            if (iconSize is null)
            {
                iconSize = _IconSize;
            }

            _Children.Clear();
            _Folders.Clear();

            FileObject      fobj;
            DirectoryObject dobj;
            IShellItem      shitem = null;
            IShellFolder    shfld;
            IEnumIDList     enumer;

            MemPtr mm;
            var    mm2 = new MemPtr();

            string fp;

            string pname = ParsingName;

            if (pname is object && pname.LastIndexOf(@"\") == pname.Length - 1)
            {
                pname = pname.Substring(0, pname.Length - 1);
            }

            var argriid = Guid.Parse(ShellIIDGuid.IShellItem);
            var res     = NativeShell.SHCreateItemFromParsingName(ParsingName, IntPtr.Zero, ref argriid, ref shitem);

            _IconSize = (StandardIcons)iconSize;

            int?argiIndex = null;

            _Icon      = Resources.GetFileIcon(ParsingName, FileObject.StandardToSystem(_IconSize), iIndex: ref argiIndex);
            _IconImage = Resources.GetFileIconWPF(ParsingName, FileObject.StandardToSystem(_IconSize));

            if (res == HResult.Ok)
            {
                var argbhid  = Guid.Parse(ShellBHIDGuid.ShellFolderObject);
                var argriid1 = Guid.Parse(ShellIIDGuid.IShellFolder2);

                shitem.BindToHandler(IntPtr.Zero, ref argbhid, ref argriid1, out shfld);
                _SysInterface = shfld;

                shfld.EnumObjects(IntPtr.Zero, ShellFolderEnumerationOptions.Folders | ShellFolderEnumerationOptions.IncludeHidden | ShellFolderEnumerationOptions.NonFolders | ShellFolderEnumerationOptions.InitializeOnFirstNext, out enumer);

                if (enumer != null)
                {
                    var    glist = new List <string>();
                    uint   cf;
                    var    x = IntPtr.Zero;
                    string pout;

                    // mm.AllocCoTaskMem((MAX_PATH * 2) + 8)

                    mm2.Alloc(NativeShell.MAX_PATH * 2 + 8);

                    do
                    {
                        cf = 0U;
                        mm2.ZeroMemory(0L, NativeShell.MAX_PATH * 2 + 8);
                        res = enumer.Next(1U, out x, out cf);
                        mm  = x;

                        if (cf == 0L)
                        {
                            break;
                        }

                        if (res != HResult.Ok)
                        {
                            break;
                        }

                        mm2.IntAt(0L) = 2;

                        // shfld.GetAttributesOf(1, mm, attr)
                        shfld.GetDisplayNameOf(mm, (uint)ShellItemDesignNameOptions.ParentRelativeParsing, mm2.handle);
                        MemPtr inv;

                        if (IntPtr.Size == 4)
                        {
                            inv = (IntPtr)mm2.IntAt(1L);
                        }
                        else
                        {
                            inv = (IntPtr)mm2.LongAt(1L);
                        }

                        if (inv.Handle != IntPtr.Zero)
                        {
                            if (inv.CharAt(0L) != '\0')
                            {
                                fp = (string)inv;
                                var lpInfo = new SHFILEINFO();

                                // Dim sgfin As ShellFileGetAttributesOptions = 0,
                                // sgfout As ShellFileGetAttributesOptions = 0

                                int iFlags = User32.SHGFI_PIDL | User32.SHGFI_ATTRIBUTES;
                                lpInfo.dwAttributes = 0;
                                x = User32.SHGetItemInfo(mm.Handle, 0, ref lpInfo, Marshal.SizeOf(lpInfo), iFlags);
                                if (ParsingName is object)
                                {
                                    if (pname.LastIndexOf(@"\") == pname.Length - 1)
                                    {
                                        pname = pname.Substring(0, pname.Length - 1);
                                    }
                                    pout = $@"{pname}\{fp}";
                                }
                                else
                                {
                                    pout = fp;
                                }

                                if (lpInfo.dwAttributes == 0)
                                {
                                    lpInfo.dwAttributes = (int)FileTools.GetFileAttributes(pout);
                                }

                                FileAttributes drat = (FileAttributes)(int)(lpInfo.dwAttributes);
                                if ((lpInfo.dwAttributes & (int)FileAttributes.Directory) == (int)FileAttributes.Directory && !File.Exists(pout))
                                {
                                    dobj          = new DirectoryObject(pout, _IsSpecial, false);
                                    dobj.Parent   = this;
                                    dobj.IconSize = _IconSize;
                                    _Children.Add(dobj);
                                    _Folders.Add(dobj);
                                }
                                else
                                {
                                    fobj          = new FileObject(pout, _IsSpecial, true, _IconSize);
                                    fobj.Parent   = this;
                                    fobj.IconSize = _IconSize;
                                    _Children.Add(fobj);
                                }
                            }

                            inv.CoTaskMemFree();
                        }

                        mm.CoTaskMemFree();
                    }while (res == HResult.Ok);
                    mm2.Free();
                }
            }

            OnPropertyChanged(nameof(Folders));
            OnPropertyChanged(nameof(Icon));
            OnPropertyChanged(nameof(IconImage));
            OnPropertyChanged(nameof(IconSize));
            OnPropertyChanged(nameof(ParsingName));
            OnPropertyChanged(nameof(DisplayName));
        }
Exemple #9
0
 public static T BindToHandler <T>(this IShellItem psi, IBindCtx pbc, Guid bhid) where T : class
 {
     return((T)psi.BindToHandler(pbc, bhid, typeof(T).GUID));
 }