/// <summary> /// Returns the <see cref="KnownFolderNative"/> object that represents the desktop object. /// </summary> /// <returns></returns> public static KnownFolderNative GetDesktop() { IntPtr pidl = default(IntPtr); try { NativeMethods.SHGetKnownFolderIDList(KnownFolderGuids.FOLDERID_Desktop, KNOWN_FOLDER_FLAG.KF_NO_FLAGS, IntPtr.Zero, out pidl); if (pidl != default(IntPtr)) { return(KnownFolderHelper.FromPIDL(pidl)); } } finally { if (pidl != default(IntPtr)) { NativeMethods.ILFree(pidl); } } return(null); }
/// <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> /// <param name="IsSpecialPath"></param> /// <returns>A known folder representing the specified name.</returns> public static KnownFolderNative FromPath(string path, bool?IsSpecialPath) { if (string.IsNullOrEmpty(path) == true) { throw new ArgumentNullException("'path' parameter cannot be Empty or Null."); } if (IsSpecialPath == null) { IsSpecialPath = (ShellHelpers.IsSpecialPath(path) == ShellHelpers.SpecialPath.IsSpecialPath); } if (IsSpecialPath == true) { // Get the KnownFolderId Guid for this special folder var kf_guid = new Guid(path.Substring(KF_IID.IID_Prefix.Length)); try { var ret = FromKnownFolderGuid(kf_guid); if (ret != null) { return(ret); } } catch { // continue iteration on exceptional errors } } IntPtr pidl = default(IntPtr); try { pidl = ShellHelpers.PIDLFromPath(path); if (pidl == default(IntPtr)) { // try one more time with a trailing \0 pidl = ShellHelpers.PidlFromParsingName(path); } if (pidl != default(IntPtr)) { // It's probably a special folder, try to get it var kf = KnownFolderHelper.FromPIDL(pidl); if (kf != null) { return(kf); } } } finally { pidl = PidlManager.ILFree(pidl); } return(null); }