private static System.Collections.ObjectModel.ReadOnlyCollection <IKnownFolder> GetAllFolders()
        {
            // Should this method be thread-safe?? (It'll take a while
            // to get a list of all the known folders, create the managed wrapper
            // and return the read-only collection.

            var foldersList = new WinCopies.Collections.
#if WAPICP3
                              Generic.
#endif
                              ArrayBuilder <IKnownFolder>();

            IntPtr folders = IntPtr.Zero;

            try
            {
                new KnownFolderManagerClass().GetFolderIds(out folders, out uint count);

                if (count > 0 && folders != IntPtr.Zero)
                {
                    // Loop through all the KnownFolderID elements
                    for (int i = 0; i < count; i++)
                    {
                        // Convert to Guid
                        var knownFolderID = (Guid)Marshal.PtrToStructure(new IntPtr(folders.ToInt64() + (Marshal.SizeOf(typeof(Guid)) * i)), typeof(Guid));

                        IKnownFolder kf = KnownFolderHelper.FromKnownFolderIdInternal(knownFolderID);

                        // Add to our collection if it's not null (some folders might not exist on the system
                        // or we could have an exception that resulted in the null return from above method call
                        if (kf != null)
                        {
                            _ = foldersList.AddLast(kf);
                        }
                    }
                }
            }

            finally
            {
                if (folders != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(folders);
                }
            }

            return(new System.Collections.ObjectModel.ReadOnlyCollection <IKnownFolder>(foldersList.ToList()));
        }
Exemple #2
0
        private static ReadOnlyCollection <IKnownFolder> GetAllFolders()
        {
            // Should this method be thread-safe?? (It'll take a while
            // to get a list of all the known folders, create the managed wrapper
            // and return the read-only collection.

            IList <IKnownFolder> foldersList = new List <IKnownFolder>();
            uint   count;
            IntPtr folders = IntPtr.Zero;

            try
            {
                KnownFolderManagerClass knownFolderManager = new KnownFolderManagerClass();
                knownFolderManager.GetFolderIds(out folders, out count);

                if (count > 0 && folders != IntPtr.Zero)
                {
                    // Loop through all the KnownFolderID elements
                    for (int i = 0; i < count; i++)
                    {
                        // Read the current pointer
                        IntPtr current = new IntPtr(folders.ToInt64() + (Marshal.SizeOf(typeof(Guid)) * i));

                        // Convert to Guid
                        Guid knownFolderID = (Guid)Marshal.PtrToStructure(current, typeof(Guid));

                        IKnownFolder kf = KnownFolderHelper.FromKnownFolderIdInternal(knownFolderID);

                        // Add to our collection if it's not null (some folders might not exist on the system
                        // or we could have an exception that resulted in the null return from above method call
                        if (kf != null)
                        {
                            foldersList.Add(kf);
                        }
                    }
                }
            }
            finally
            {
                if (folders != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(folders);
                }
            }

            return(new ReadOnlyCollection <IKnownFolder>(foldersList));
        }