/// <summary> /// Returns the known folder given its canonical name. /// </summary> /// <param name="canonicalName">A non-localized canonical name for the known folder, such as MyComputer.</param> /// <returns>A known folder representing the specified name.</returns> /// <exception cref="System.ArgumentException">Thrown if the given canonical name is invalid or if the KnownFolder could not be created.</exception> public static IKnownFolder FromCanonicalName(string canonicalName) { IKnownFolderNative knownFolderNative; IKnownFolderManager knownFolderManager = (IKnownFolderManager) new KnownFolderManagerClass(); knownFolderManager.GetFolderByName(canonicalName, out knownFolderNative); IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative); if (kf != null) { return(kf); } else { throw new ArgumentException("Canonical name is invalid.", "canonicalName"); } }
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) => 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)); }
private static IKnownFolder GetKnownFolder(Guid guid) { return(KnownFolderHelper.FromKnownFolderId(guid)); }
/// <summary> /// Returns a known folder given its shell namespace parsing name, such as /// <c>::{645FF040-5081-101B-9F08-00AA002F954E}</c> for the Recycle Bin. /// </summary> /// <param name="parsingName">The parsing name (or path) for the requested known folder.</param> /// <returns>A known folder representing the specified name.</returns> /// <exception cref="System.ArgumentException">Thrown if the given parsing name is invalid.</exception> public static IKnownFolder FromParsingName(string parsingName) { IntPtr pidl = IntPtr.Zero; IntPtr pidl2 = IntPtr.Zero; try { pidl = ShellHelper.PidlFromParsingName(parsingName); if (pidl == IntPtr.Zero) { throw new ArgumentException("Parsing name is invalid.", "parsingName"); } // It's probably a special folder, try to get it IKnownFolderNative knownFolderNative = KnownFolderHelper.FromPIDL(pidl); if (knownFolderNative != null) { IKnownFolder kf = KnownFolderHelper.GetKnownFolder(knownFolderNative); if (kf != null) { return(kf); } else { throw new ArgumentException("Parsing name is invalid.", "parsingName"); } } else { // No physical storage was found for this known folder // We'll try again with a different name // try one more time with a trailing \0 pidl2 = ShellHelper.PidlFromParsingName(parsingName.PadRight(1, '\0')); if (pidl2 == IntPtr.Zero) { throw new ArgumentException("Parsing name is invalid.", "parsingName"); } IKnownFolder kf = KnownFolderHelper.GetKnownFolder(KnownFolderHelper.FromPIDL(pidl)); if (kf != null) { return(kf); } else { throw new ArgumentException("Parsing name is invalid.", "parsingName"); } } } finally { ShellNativeMethods.ILFree(pidl); ShellNativeMethods.ILFree(pidl2); } }
/// <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) => KnownFolderHelper.FromParsingName(path);