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)); }
private static IKnownFolder GetKnownFolder(Guid guid) { return(KnownFolderHelper.FromKnownFolderId(guid)); }
/// <summary> /// Returns a known folder given its shell path, such as <c>C:\users\public\documents</c> or /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin. /// </summary> /// <param name="path">The path for the requested known folder; either a physical path or a virtual path.</param> /// <returns>A known folder representing the specified name.</returns> public static IKnownFolder FromPath(string path) { return(KnownFolderHelper.FromParsingName(path)); }