Beispiel #1
0
 private static IntPtr GetPidl([NotNull] DirectoryInfo directoryInfo)
 {
     if (directoryInfo.Parent != null)
     {
         var oParentFolder = GetParentFolder(directoryInfo.Parent.FullName);
         if (null == oParentFolder)
         {
             return(IntPtr.Zero);
         }
         uint        pchEaten      = 0;
         Enums.Sfgao pdwAttributes = 0;
         IntPtr      pPidl;
         oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, directoryInfo.Name, ref pchEaten, out pPidl, ref pdwAttributes);
         return(pPidl);
     }
     return(IntPtr.Zero);
 }
Beispiel #2
0
        private static IShellFolder GetParentFolder(string folderName)
        {
            if (null == _oParentFolder)
            {
                var oDesktopFolder = (IShellFolder)SHGetDesktopFolder.GetDesktopFolder();
                if (null == oDesktopFolder)
                {
                    return(null);
                }
                IntPtr      pPidl;
                uint        pchEaten      = 0;
                Enums.Sfgao pdwAttributes = 0;
                var         nResult       = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out pPidl, ref pdwAttributes);
                if (SOk != nResult)
                {
                    return(null);
                }
                var pStrRet = Marshal.AllocCoTaskMem(MaxPath * 2 + 4);
                Marshal.WriteInt32(pStrRet, 0, 0);
                nResult = _oDesktopFolder.GetDisplayNameOf(pPidl, Enums.Shgno.FORPARSING, pStrRet);
                var strFolder = new StringBuilder(MaxPath);
                DllImports.StrRetToBuf(pStrRet, pPidl, strFolder, MaxPath);
                Marshal.FreeCoTaskMem(pStrRet);
                pStrRet          = IntPtr.Zero;
                _strParentFolder = strFolder.ToString();
                IntPtr pUnknownParentFolder;
                nResult = oDesktopFolder.BindToObject(pPidl, IntPtr.Zero, ref _iidIShellFolder, out pUnknownParentFolder);
                Marshal.FreeCoTaskMem(pPidl);
                if (SOk != nResult)
                {
                    return(null);
                }
                _oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder));
            }

            return(_oParentFolder);
        }