Esempio n. 1
0
        public IntPtr GetDisplayName(SIGDN sigdnName)
        {
            if (sigdnName == SIGDN.FILESYSPATH)
            {
                StringBuilder result = new StringBuilder(512);
                if (!Shell32.SHGetPathFromIDList(m_Pidl, result))
                {
                    throw new ArgumentException();
                }
                return(Marshal.StringToHGlobalUni(result.ToString()));
            }
            else
            {
                IShellFolder  parentFolder = GetParent().GetIShellFolder();
                IntPtr        childPidl    = Shell32.ILFindLastID(m_Pidl);
                StringBuilder builder      = new StringBuilder(512);
                STRRET        strret       = new STRRET();

                parentFolder.GetDisplayNameOf(childPidl,
                                              (SHGNO)((int)sigdnName & 0xffff), out strret);
                ShlWapi.StrRetToBuf(ref strret, childPidl, builder,
                                    (uint)builder.Capacity);
                return(Marshal.StringToHGlobalUni(builder.ToString()));
            }
        }
Esempio n. 2
0
        private static void GetChilds(IShellFolder desktop)
        {
            Console.WriteLine("===============GetChilds================");

            //循环查找子项
            Microsoft.WindowsAPICodePack.Shell.IEnumIDList Enum = null;
            IEnumIDList EnumPtr;
            IntPtr      pidlSub;
            uint        celtFetched;

            if (desktop.EnumObjects(IntPtr.Zero, ShellNativeMethods.ShellFolderEnumerationOptions.Folders | ShellNativeMethods.ShellFolderEnumerationOptions.IncludeHidden | ShellNativeMethods.ShellFolderEnumerationOptions.Storage, out EnumPtr) == CONST.S_OK)
            {
                while (EnumPtr.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == CONST.S_FALSE)
                {
                    IntPtr strr;
                    desktop.GetDisplayNameOf(ref pidlSub, (uint)SHGNO.INFOLDER, out strr);
                    string name = Marshal.PtrToStringAuto(strr);

                    //SHFILEINFO info = new SHFILEINFO();
                    //Shell32.SHGetFileInfo(pidlSub, 0, ref info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.ICONLOCATION);

                    //IShellFolder iSub;
                    //desktop.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub);

                    Console.WriteLine(name);
                }
            }
            Console.WriteLine();
            Console.WriteLine();
        }
Esempio n. 3
0
        public static string GetDisplayName(IntPtr pIDL, bool fDisplayName)
        {
            IShellFolder ppv  = null;
            Guid         riid = ExplorerGUIDs.IID_IShellFolder;

            try {
                IntPtr ptr;
                STRRET strret;
                if ((PInvoke.SHBindToParent(pIDL, riid, out ppv, out ptr) != 0) || (ppv == null))
                {
                    return(string.Empty);
                }
                uint          uFlags = fDisplayName ? 0 : 0x8000u;
                StringBuilder pszBuf = new StringBuilder(260);
                if (ppv.GetDisplayNameOf(ptr, uFlags, out strret) == 0)
                {
                    PInvoke.StrRetToBuf(ref strret, ptr, pszBuf, pszBuf.Capacity);
                }
                return(pszBuf.ToString());
            }
            catch {
            }
            finally {
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
            }
            return(string.Empty);
        }
Esempio n. 4
0
        protected override bool RunDialog(System.IntPtr hwndOwner)
        {
            string m_DisplayName = "";
            string sDisplay;
            IntPtr ptrRet;

            // Get IMalloc interface
            WindowsAPI.SHGetMalloc(out ptrRet);

            Type    mallocType = System.Type.GetType("IMalloc");
            IMalloc pMalloc    = ( IMalloc )Marshal.GetTypedObjectForIUnknown(ptrRet, mallocType);
            IntPtr  pidlRoot   = IntPtr.Zero;

            BROWSEINFO bi = new BROWSEINFO();

            bi.hwndOwner      = hwndOwner;
            bi.pidlRoot       = pidlRoot; //TODO: Root`s selection
            bi.pszDisplayName = new string( ' ', 256 );
            bi.lpszTitle      = Title;
            bi.ulFlags        = GetFlagsValue();
            bi.lParam         = 0;
            bi.lpfn           = new WindowsAPI.BrowseCallbackProc(this.HookProc);

            IntPtr pidlSelected;

            pidlSelected  = WindowsAPI.SHBrowseForFolder(ref bi);
            m_DisplayName = bi.pszDisplayName.ToString();

            // if display name is whitespace then return FAIL
            if (m_DisplayName.Trim() == string.Empty)
            {
                return(false);
            }

            IShellFolder isf = GetDesktopFolder();

            STRRET ptrDisplayName;

            isf.GetDisplayNameOf(pidlSelected,
                                 (uint)SHGNO.SHGDN_NORMAL | (uint)SHGNO.SHGDN_FORPARSING,
                                 out ptrDisplayName);

            WindowsAPI.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay);
            m_sFullName = sDisplay;

            if (pidlRoot != IntPtr.Zero)
            {
                pMalloc.Free(pidlRoot);
            }

            if (pidlSelected != IntPtr.Zero)
            {
                pMalloc.Free(pidlSelected);
            }

            Marshal.ReleaseComObject(isf);
            Marshal.ReleaseComObject(pMalloc);

            return(true);
        }
Esempio n. 5
0
        private static void GetChilds(IShellFolder desktop)
        {
            Console.WriteLine("===============GetChilds================");

            //循环查找子项
            Microsoft.WindowsAPICodePack.Shell.IEnumIDList Enum = null;
            IEnumIDList EnumPtr;
            IntPtr pidlSub;
            uint celtFetched;

            if (desktop.EnumObjects(IntPtr.Zero, ShellNativeMethods.ShellFolderEnumerationOptions.Folders | ShellNativeMethods.ShellFolderEnumerationOptions.IncludeHidden | ShellNativeMethods.ShellFolderEnumerationOptions.Storage, out EnumPtr) == CONST.S_OK)
            {
                while (EnumPtr.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == CONST.S_FALSE)
                {
                    IntPtr strr;
                    desktop.GetDisplayNameOf(ref pidlSub, (uint)SHGNO.INFOLDER, out strr);
                    string name = Marshal.PtrToStringAuto(strr);

                    //SHFILEINFO info = new SHFILEINFO();
                    //Shell32.SHGetFileInfo(pidlSub, 0, ref info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.ICONLOCATION);

                    //IShellFolder iSub;
                    //desktop.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub);

                    Console.WriteLine(name);
                }
            }
            Console.WriteLine();
            Console.WriteLine();
        }
        public static string GetDisplayNameOf(this IShellFolder shellFolder, IntPtr pidl, SHGDNF uFlags)
        {
            shellFolder.GetDisplayNameOf(pidl, SHGDNF.FORPARSING, out var str);

            NativeMethods.StrRetToBSTR(ref str, pidl, out var buffer);

            return(buffer);
        }
Esempio n. 7
0
 /// <summary>
 /// 获取显示名称
 /// </summary>
 public static string GetNameByIShell(IShellFolder Root, IntPtr pidlSub)
 {
     IntPtr strr = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);
     Marshal.WriteInt32(strr, 0, 0);
     StringBuilder buf = new StringBuilder(MAX_PATH);
     Root.GetDisplayNameOf(pidlSub, SHGNO.INFOLDER, strr);
     Shlwapi.StrRetToBuf(strr, pidlSub, buf, MAX_PATH);
     return buf.ToString();
 }
Esempio n. 8
0
        /// <summary>
        /// 获取显示名称
        /// </summary>
        public static string GetNameByIShell(IShellFolder Root, IntPtr pidlSub)
        {
            IntPtr strr = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);

            Marshal.WriteInt32(strr, 0, 0);
            StringBuilder buf = new StringBuilder(MAX_PATH);

            Root.GetDisplayNameOf(pidlSub, SHGNO.INFOLDER, strr);
            Shlwapi.StrRetToBuf(strr, pidlSub, buf, MAX_PATH);
            return(buf.ToString());
        }
Esempio n. 9
0
        /// <summary> 获取路径 </summary>
        public static string GetPathByIShell(IShellFolder Root, IntPtr pidlSub)
        {
            IntPtr strr = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);

            Marshal.WriteInt32(strr, 0, 0);
            StringBuilder buf = new StringBuilder(MAX_PATH);

            Root.GetDisplayNameOf(pidlSub, SHGNO.FORADDRESSBAR | SHGNO.FORPARSING, strr);
            API.StrRetToBuf(strr, pidlSub, buf, MAX_PATH);
            Marshal.FreeCoTaskMem(strr);
            return(buf.ToString());
        }
Esempio n. 10
0
        public static string GetDisplayName(IShellFolder shellFolder, IntPtr pIDLLast, bool fDisplayName)
        {
            STRRET        strret;
            uint          uFlags = fDisplayName ? 0 : 0x8000u;
            StringBuilder pszBuf = new StringBuilder(260);

            if (shellFolder.GetDisplayNameOf(pIDLLast, uFlags, out strret) == 0)
            {
                PInvoke.StrRetToBuf(ref strret, pIDLLast, pszBuf, pszBuf.Capacity);
            }
            return(pszBuf.ToString());
        }
Esempio n. 11
0
        public Int32 ShouldShow(
            Object psf,        // A pointer to the folder's IShellFolder interface.
            IntPtr pidlFolder, // The folder's PIDL.
            IntPtr pidlItem)   // The item's PIDL.
        {
            // check extension, and if not ok return 1 (S_FALSE)

            // get display name of item
            IShellFolder isf = (IShellFolder)psf;

            ShellApi.STRRET ptrDisplayName;
            isf.GetDisplayNameOf(pidlItem, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING,
                                 out ptrDisplayName);

            String sDisplay;

            ShellApi.StrRetToBSTR(ref ptrDisplayName, (IntPtr)0, out sDisplay);

            // check if item is file or folder
            IntPtr[] aPidl = new IntPtr[1];
            aPidl[0] = pidlItem;
            uint Attrib;

            Attrib = (uint)ShellApi.SFGAO.SFGAO_FOLDER;

            int temp;

            temp = isf.GetAttributesOf(1, aPidl, ref Attrib);

            // if item is a folder, accept
            if ((Attrib & (uint)ShellApi.SFGAO.SFGAO_FOLDER) == (uint)ShellApi.SFGAO.SFGAO_FOLDER)
            {
                return(0);
            }

            // if item is file, check if it has a valid extension
            for (int i = 0; i < ValidExtension.Length; i++)
            {
                if (sDisplay.ToUpper().EndsWith("." + ValidExtension[i].ToUpper()))
                {
                    return(0);
                }
            }

            return(1);
        }
Esempio n. 12
0
        /// <summary>
        /// Gets the parent folder
        /// </summary>
        /// <param name="folderName">Folder path</param>
        /// <returns>IShellFolder for the folder (relative from the desktop)</returns>
        private IShellFolder GetParentFolder(string folderName)
        {
            if (null == _oParentFolder)
            {
                IShellFolder oDesktopFolder = GetDesktopFolder();
                if (null == oDesktopFolder)
                {
                    return(null);
                }

                // Get the PIDL for the folder file is in
                IntPtr pPIDL         = IntPtr.Zero;
                uint   pchEaten      = 0;
                SFGAO  pdwAttributes = 0;
                int    nResult       = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out pPIDL, ref pdwAttributes);
                if (S_OK != nResult)
                {
                    return(null);
                }

                IntPtr pStrRet = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4);
                Marshal.WriteInt32(pStrRet, 0, 0);
                nResult = _oDesktopFolder.GetDisplayNameOf(pPIDL, SHGNO.FORPARSING, pStrRet);
                StringBuilder strFolder = new StringBuilder(MAX_PATH);
                Shell32.StrRetToBuf(pStrRet, pPIDL, strFolder, MAX_PATH);
                Marshal.FreeCoTaskMem(pStrRet);
                pStrRet          = IntPtr.Zero;
                _strParentFolder = strFolder.ToString();

                // Get the IShellFolder for folder
                IntPtr pUnknownParentFolder = IntPtr.Zero;
                nResult = oDesktopFolder.BindToObject(pPIDL, IntPtr.Zero, ref IID_IShellFolder, out pUnknownParentFolder);
                // Free the PIDL first
                Marshal.FreeCoTaskMem(pPIDL);
                if (S_OK != nResult)
                {
                    return(null);
                }
                _oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder));
            }

            return(_oParentFolder);
        }
Esempio n. 13
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);
        }
Esempio n. 14
0
 public static string GetDisplayName(IShellFolder shellFolder, IntPtr pIDLLast, bool fDisplayName) {
     STRRET strret;
     uint uFlags = fDisplayName ? 0 : 0x8000u;
     StringBuilder pszBuf = new StringBuilder(260);
     if(shellFolder.GetDisplayNameOf(pIDLLast, uFlags, out strret) == 0) {
         PInvoke.StrRetToBuf(ref strret, pIDLLast, pszBuf, pszBuf.Capacity);
     }
     return pszBuf.ToString();
 }
        public void ShowDialog()
        {
            m_FullName    = "";
            m_DisplayName = "";

            // Get shell's memory allocator, it is needed to free some memory later
            IMalloc pMalloc;

            pMalloc = ShellFunctions.GetMalloc();

            IntPtr pidlRoot;

            if (RootType == RootTypeOptions.BySpecialFolder)
            {
                ShellApi.SHGetFolderLocation(hwndOwner, (int)RootSpecialFolder, UserToken, 0, out pidlRoot);
            }
            else // m_RootType = RootTypeOptions.ByPath
            {
                uint iAttribute;
                ShellApi.SHParseDisplayName(RootPath, IntPtr.Zero, out pidlRoot, 0, out iAttribute);
            }

            ShellApi.BROWSEINFO bi = new ShellApi.BROWSEINFO();

            bi.hwndOwner      = hwndOwner;
            bi.pidlRoot       = pidlRoot;
            bi.pszDisplayName = new String(' ', 256);
            bi.lpszTitle      = Title;
            bi.ulFlags        = (uint)DetailsFlags;
            bi.lParam         = 0;
            bi.lpfn           = new ShellApi.BrowseCallbackProc(myBrowseCallbackProc);

            // Show dialog
            IntPtr pidlSelected;

            pidlSelected = ShellApi.SHBrowseForFolder(ref bi);

            // Save the display name
            m_DisplayName = bi.pszDisplayName;

            IShellFolder isf = ShellFunctions.GetDesktopFolder();

            ShellApi.STRRET ptrDisplayName;
            isf.GetDisplayNameOf(pidlSelected, (uint)ShellApi.SHGNO.SHGDN_NORMAL | (uint)ShellApi.SHGNO.SHGDN_FORPARSING,
                                 out ptrDisplayName);

            String sDisplay;

            ShellApi.StrRetToBSTR(ref ptrDisplayName, pidlRoot, out sDisplay);
            m_FullName = sDisplay;

            if (pidlRoot != IntPtr.Zero)
            {
                pMalloc.Free(pidlRoot);
            }

            if (pidlSelected != IntPtr.Zero)
            {
                pMalloc.Free(pidlSelected);
            }

            Marshal.ReleaseComObject(isf);
            Marshal.ReleaseComObject(pMalloc);
        }
Esempio n. 16
0
        //色々サンプルを書きたいのよ
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.hoge.ExplorerBrowserControl.Navigate((ShellObject)KnownFolders.Desktop);
                return;

                IntPtr PDesktop;
                Shell32Wrapper.SHGetDesktopFolder(out PDesktop);

                IShellFolder SHDesktop = (IShellFolder)Marshal.GetTypedObjectForIUnknown(PDesktop, typeof(IShellFolder));

                IntPtr strr = Marshal.AllocCoTaskMem(Shell32Wrapper.MAX_PATH * 2 + 4);
                Marshal.WriteInt32(strr, 0, 0);

                IntPtr ptrPid;
                Shell32Wrapper.SHGetSpecialFolderLocation(IntPtr.Zero, (int)SpecialFolderID.CSIDL_DRIVES, out ptrPid);

                //なんかDesktop自身の名前は取得方法がわからん
                //if (SHDesktop.GetDisplayNameOf(desktop, SHGNO.NORMAL, strr) == Shell32Wrapper.S_OK)
                if (SHDesktop.GetDisplayNameOf(ptrPid, SHGNO.NORMAL, strr) == Shell32Wrapper.S_OK)
                {
                    StringBuilder buf = new StringBuilder(Shell32Wrapper.MAX_PATH);
                    ShlwapiWrapper.StrRetToBuf(strr, PDesktop, buf, Shell32Wrapper.MAX_PATH);
                    MessageBox.Show(buf.ToString());
                }
                Marshal.FreeCoTaskMem(strr);


                //対象ファイルの親のシェルフォルダの PIDL を取得する
                IntPtr PFolder;
                uint   fcharcnt = 0;
                SFGAO  fattr    = SFGAO.BROWSABLE;
                if (SHDesktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, @"C:\temp", ref fcharcnt, out PFolder, ref fattr) == Shell32Wrapper.S_OK)
                {
                    //対象ファイルの親のシェルフォルダのポインタを取得する
                    IntPtr PPV;
                    if (SHDesktop.BindToObject(PFolder, IntPtr.Zero, GUIDs.IID_IShellFolder, out PPV) == Shell32Wrapper.S_OK)
                    {
                        //対象ファイルの親のシェルフォルダ IShellFolder を取得する
                        IShellFolder SHFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(PPV, typeof(IShellFolder));

                        //対象ファイルの PIDL (親のシェルフォルダからの相対 PIDL)を取得する
                        IntPtr PFile;
                        if (SHFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, "a.vbs", ref fcharcnt, out PFile, ref fattr) == Shell32Wrapper.S_OK)
                        {
                            //対象ファイルの IContextMenu へのポインタを取得する
                            IntPtr[] children = new IntPtr[] { PFile };
                            IntPtr   PContext;
                            if (SHFolder.GetUIObjectOf(IntPtr.Zero, (uint)children.Length, children, GUIDs.IID_IContextMenu, IntPtr.Zero, out PContext) == Shell32Wrapper.S_OK)
                            {
                                //対象ファイルの IContextMenu を取得する
                                IContextMenu CContext = (IContextMenu)Marshal.GetTypedObjectForIUnknown(PContext, typeof(IContextMenu));

                                //対象ファイルの IContextMenu2, IContextMenu3 のポインタを取得する
                                IntPtr PContext2, PContext3;
                                Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu2, out PContext2);
                                Marshal.QueryInterface(PContext, ref GUIDs.IID_IContextMenu3, out PContext3);

                                IContextMenu2 CContext2 = (IContextMenu2)Marshal.GetTypedObjectForIUnknown(PContext2, typeof(IContextMenu2));
                                IContextMenu3 CContext3 = (IContextMenu3)Marshal.GetTypedObjectForIUnknown(PContext3, typeof(IContextMenu3));

                                //ポップアップメニューを作成する
                                IntPtr PPopup = User32Wrapper.CreatePopupMenu();

                                //ポップアップメニューに、コンテキストメニュー IContextMenu を追加する
                                CMF ContextMenuFlag = CMF.EXPLORE | CMF.CANRENAME;
                                CContext.QueryContextMenu(PPopup, 0, Shell32Wrapper.CMD_FIRST, Shell32Wrapper.CMD_LAST, ContextMenuFlag);

                                //ポップアップメニューを表示する
                                //呼び出しをブロックします
                                uint selected = User32Wrapper.TrackPopupMenuEx(PPopup, TPM.RETURNCMD, 0, 0, new System.Windows.Interop.WindowInteropHelper(this).Handle, IntPtr.Zero);
                                if (selected >= Shell32Wrapper.CMD_FIRST)
                                {
                                    uint cmdidx = selected - Shell32Wrapper.CMD_FIRST;


                                    Helper.InvokeCommand(CContext, cmdidx, @"C:\temp", new Point(0, 0));
                                }

                                User32Wrapper.DestroyMenu(PPopup);

                                Marshal.ReleaseComObject(CContext3);
                                Marshal.ReleaseComObject(CContext2);

                                Marshal.Release(PContext3);
                                Marshal.Release(PContext2);

                                Marshal.ReleaseComObject(CContext);
                            }
                            Marshal.Release(PContext);
                        }
                        Marshal.FreeCoTaskMem(PFile);
                        Marshal.ReleaseComObject(SHFolder);
                    }
                    Marshal.Release(PPV);
                }
                //なんでかExceptionが発生するのでコメントアウト ← 解放するメソッドを間違えていた
                //Marshal.Release(PIDLParent);
                Marshal.FreeCoTaskMem(PFolder);
                Marshal.ReleaseComObject(SHDesktop);
                Marshal.Release(PDesktop);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Esempio n. 17
0
 public int GetDisplayNameOf(IntPtr pidl, ShellAPI.SHGNO uFlags, IntPtr lpName)
 {
     checkDisposed();
     return(_iShellFolder.GetDisplayNameOf(pidl, uFlags, lpName));
 }
Esempio n. 18
0
 private static IntPtr GetAbsolutePidl(IShellFolder folder, IntPtr relativePidl)
 {
     IntPtr ptr;
     IShellFolder desktopFolder = GetDesktopFolder();
     try
     {
         string lpszDisplayName = folder.GetDisplayNameOf(relativePidl, SHGNO.SHGDN_FORPARSING);
         ptr = desktopFolder.ParseDisplayName(IntPtr.Zero, lpszDisplayName);
     }
     finally
     {
         Marshal.ReleaseComObject(desktopFolder);
     }
     return ptr;
 }
Esempio n. 19
0
 public static string ExtractDisplayName( IShellFolder shellFolder,  IntPtr pidlRelative, IntPtr pidlAbsolute )
 {
     try
     {
         STRRET ptrString;
         shellFolder.GetDisplayNameOf( pidlRelative,
             (uint)SHGDN.SHGDN_NORMAL, out ptrString );
         StringBuilder strDisplay = new StringBuilder( (int)MAX.PATH );
         StrRetToBuf( ref ptrString, pidlAbsolute, strDisplay, (uint)strDisplay.Capacity );
         return strDisplay.ToString( );
     }
     catch
     {
         throw;
     }
 }
Esempio n. 20
0
 private static bool CheckDisplayName(IShellFolder shellFolder, IntPtr pIDLLast, Regex re) {
     if(pIDLLast != IntPtr.Zero) {
         STRRET strret;
         uint uFlags = 0;
         StringBuilder pszBuf = new StringBuilder(260);
         if(shellFolder.GetDisplayNameOf(pIDLLast, uFlags, out strret) == 0) {
             PInvoke.StrRetToBuf(ref strret, pIDLLast, pszBuf, pszBuf.Capacity);
         }
         if(pszBuf.Length > 0) {
             return re.IsMatch(pszBuf.ToString());
         }
     }
     return false;
 }
Esempio n. 21
0
        public static List <ShellItem> GetItems(Guid FolderID) //Получает список элементов каталога по GUID
        {
            IntPtr           p       = IntPtr.Zero;
            IShellFolder     pFolder = null;
            IEnumIDList      pEnum   = null;
            IntPtr           pItem   = IntPtr.Zero;
            IntPtr           lpStr   = IntPtr.Zero;
            STRRET           strret;
            Guid             guid  = typeof(IShellFolder).GUID;
            List <ShellItem> items = new List <ShellItem>();
            ShellItem        si;

            try
            {
                int hr = NativeMethods.SHGetKnownFolderIDList(ref FolderID, 0, IntPtr.Zero, out p);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }

                hr = NativeMethods.SHBindToObject(null, p, null, ref guid, out pFolder);
                if (hr != 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }

                pFolder.EnumObjects(IntPtr.Zero, NativeMethods.SHCONTF_FOLDERS | NativeMethods.SHCONTF_NONFOLDERS, out pEnum);

                while (true)
                {
                    pItem = IntPtr.Zero;
                    uint res = pEnum.Next(1, out pItem, IntPtr.Zero);
                    if (res != 0)
                    {
                        break;
                    }
                    si = new ShellItem();

                    //display name
                    lpStr  = IntPtr.Zero;
                    strret = new STRRET();
                    pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_NORMAL, out strret);
                    hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr);
                    if (hr != 0)
                    {
                        throw Marshal.GetExceptionForHR(hr);
                    }
                    string s = Marshal.PtrToStringUni(lpStr);
                    si.DisplayName = s;
                    NativeMethods.CoTaskMemFree(lpStr);

                    //path
                    lpStr  = IntPtr.Zero;
                    strret = new STRRET();
                    pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_FORPARSING, out strret);
                    hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr);
                    if (hr != 0)
                    {
                        throw Marshal.GetExceptionForHR(hr);
                    }
                    s = Marshal.PtrToStringUni(lpStr);
                    try { si.Path = new Uri(s); }
                    catch (UriFormatException) { si.Path = new Uri("file://localhost/" + s); }
                    NativeMethods.CoTaskMemFree(lpStr);

                    //icon
                    try
                    {
                        Guid         iid_IIExtractIcon = typeof(IExtractIcon).GUID;
                        IExtractIcon pExtract;
                        pFolder.GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { pItem }, ref iid_IIExtractIcon, 0, out pExtract);

                        StringBuilder sbIcon = new StringBuilder(260);
                        int           index  = 0;
                        uint          flags;
                        hr = pExtract.GetIconLocation(NativeMethods.GIL_FORSHELL, sbIcon, 260, out index, out flags);
                        if (hr == 0)
                        {
                            IntPtr hIconSmall = IntPtr.Zero, hIconLarge = IntPtr.Zero;
                            hr = pExtract.Extract(sbIcon.ToString(), (uint)index, out hIconLarge, out hIconSmall, 0x00140014);
                            if (hr == 0 && hIconSmall != IntPtr.Zero)
                            {
                                var icon   = System.Drawing.Icon.FromHandle(hIconSmall);
                                var bitmap = icon.ToBitmap();

                                using (bitmap)
                                {
                                    MemoryStream ms = new MemoryStream();
                                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                    si.Image = ms.ToArray();
                                }

                                NativeMethods.DestroyIcon(hIconSmall);
                                NativeMethods.DestroyIcon(hIconLarge);
                            }
                            else
                            {
                                si.Image = new byte[0];
                            }
                        }
                        else
                        {
                            si.Image = new byte[0];
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                        si.Image = new byte[0];
                    }
                    items.Add(si);
                    NativeMethods.CoTaskMemFree(pItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.GetType().ToString());
            }
            finally
            {
                if (p != IntPtr.Zero)
                {
                    NativeMethods.CoTaskMemFree(p);
                }
                if (pFolder != null)
                {
                    Marshal.ReleaseComObject(pFolder);
                }
                if (pEnum != null)
                {
                    Marshal.ReleaseComObject(pEnum);
                }
            }
            return(items);
        }