Ejemplo n.º 1
0
        private static string loadName(IShellFolder2 iShellFolder, IntPtr ptr, ShellAPI.SHGNO uFlags)
        {
            if (iShellFolder == null)
            {
                return(null);
            }

            IntPtr ptrStr = Marshal.AllocCoTaskMem(ShellAPI.MAX_PATH * 2 + 4);

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

            try
            {
                if (iShellFolder.GetDisplayNameOf(ptr, uFlags, ptrStr) == ShellAPI.S_OK)
                {
                    ShellAPI.StrRetToBuf(ptrStr, ptr, buf, ShellAPI.MAX_PATH);
                }
            }
            finally
            {
                if (ptrStr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptrStr);
                }
                ptrStr = IntPtr.Zero;
            }
            return(buf.ToString());
        }
Ejemplo n.º 2
0
 public int GetDisplayNameOf(IntPtr pidl, ShellAPI.SHGNO uFlags, IntPtr lpName)
 {
     checkDisposed();
     return(_iShellFolder2.GetDisplayNameOf(pidl, uFlags, lpName));
 }
Ejemplo n.º 3
0
        public void CanEnumerateDesktopFolders()
        {
            // Defines the type of items that we want to retieve below the desktop root item
            const SHCONTF flags = SHCONTF.NONFOLDERS | SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN;

            //  Get the desktop root folder.
            IntPtr        ptrDesktopFolder = default(IntPtr);
            IntPtr        enumerator       = default(IntPtr);
            IShellFolder2 iDesktopFolder   = null;

            // Enumerate over children of given shell folder item using this interface
            // https://msdn.microsoft.com/en-us/library/windows/desktop/bb761983(v=vs.85).aspx
            IEnumIDList enumIDs = null;

            try
            {
                HRESULT hr = NativeMethods.SHGetDesktopFolder(out ptrDesktopFolder);

                Assert.IsTrue(hr == HRESULT.S_OK);

                if (ptrDesktopFolder != IntPtr.Zero)
                {
                    iDesktopFolder = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(ptrDesktopFolder, typeof(IShellFolder2));
                }

                Assert.IsTrue(iDesktopFolder != null);

                //  Create an enumerator and enumerate over each item.
                hr = iDesktopFolder.EnumObjects(IntPtr.Zero, flags, out enumerator);

                Assert.IsTrue(hr == HRESULT.S_OK);

                // Convert enum IntPtr to interface
                enumIDs = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(enumerator, typeof(IEnumIDList));

                Assert.IsTrue(enumIDs != null);

                uint   fetched, count = 0;
                IntPtr apidl = default(IntPtr);

                // Get one item below desktop root at a time and process by getting its display name
                for (; enumIDs.Next(1, out apidl, out fetched) == HRESULT.S_OK; count++)
                {
                    if (fetched <= 0)  // End this loop if no more items are available
                    {
                        break;
                    }

                    IntPtr ptrStr = default(IntPtr); // get strings for this item
                    try
                    {
                        string displayName = null, parseName = null;

                        ptrStr = Marshal.AllocCoTaskMem(NativeMethods.MAX_PATH * 2 + 4);
                        Marshal.WriteInt32(ptrStr, 0, 0);
                        StringBuilder buf = new StringBuilder(NativeMethods.MAX_PATH);

                        // The apidl ITEMIDLIST structures returned in the array are relative to
                        // the IShellFolder being enumerated.
                        if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_NORMAL, ptrStr) == HRESULT.S_OK)
                        {
                            NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH);
                            displayName = buf.ToString();
                        }

                        if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_FORPARSING, ptrStr) == HRESULT.S_OK)
                        {
                            NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH);
                            parseName = buf.ToString();
                        }

                        Assert.IsFalse(string.IsNullOrEmpty(displayName));
                        Assert.IsFalse(string.IsNullOrEmpty(parseName));
                    }
                    finally
                    {
                        ptrStr = PidlManager.FreeCoTaskMem(ptrStr);
                        apidl  = PidlManager.FreeCoTaskMem(apidl);
                    }
                }

                // There should be more than one item below the desktop root item
                // 'My PC', 'Recycle Bin', and 'Network' are already three
                Assert.IsTrue(count > 2);
            }
            finally
            {
                if (enumerator != default(IntPtr))
                {
                    Marshal.Release(enumerator);
                }

                if (iDesktopFolder != null)
                {
                    Marshal.ReleaseComObject(iDesktopFolder);
                }

                if (ptrDesktopFolder != default(IntPtr))
                {
                    Marshal.Release(ptrDesktopFolder);
                }
            }
        }