//// private bool _IconResourceIdInitialized; //// private string _IconResourceId; //// //// private bool _KnownFolderIsInitialized; //// private bool _ItemTypeIsInitialized; #endregion fields #region ctors /// <summary> /// constructor to initialize only <see cref="Path_RAW"/> and /// <see cref="PathFileSystem"/> property or /// <see cref="PathSpecialItemId"/> property. /// <paramref name="rawPath"/> can be either a real path /// or special folder path starting with '::{...}' /// </summary> /// <param name="rawPath"></param> /// <param name="parsingName"></param> //// /// <param name="itemType"></param> protected BrowseItemFromPath(string rawPath, string parsingName //// ,DirectoryItemFlags itemType = DirectoryItemFlags.Unknown ) { Path_RAW = rawPath; var pathType = ShellHelpers.IsSpecialPath(parsingName); if (pathType == ShellHelpers.SpecialPath.None) { PathFileSystem = parsingName; } else { if (pathType == ShellHelpers.SpecialPath.IsSpecialPath || pathType == ShellHelpers.SpecialPath.ContainsSpecialPath) { IsSpecialParseItem = true; PathSpecialItemId = parsingName; } } //// if (itemType != DirectoryItemFlags.Unknown) //// { //// ItemType = itemType; //// _ItemTypeIsInitialized = true; //// } }
/// <summary> /// Converts a path representation 'C:\' into an /// <see cref="IntPtr"/> formated PIDL representation. /// /// The memory of the PIDL returned must be freed with /// <see cref="Marshal.FreeCoTaskMem"/> by the caller. /// </summary> /// <param name="path"></param> /// <returns></returns> public static IntPtr GetPIDLFromPath(string path) { if (string.IsNullOrEmpty(path) == true) { return(IntPtr.Zero); } IntPtr pidlPtr = default(IntPtr); // Handle Special Folder path notation if (ShellHelpers.IsSpecialPath(path) == ShellHelpers.SpecialPath.IsSpecialPath) { if (HasNoPIDL(path) == true) { return(IntPtr.Zero); } using (var kf = KnownFolderHelper.FromPath(path)) { if (kf != null) { try { kf.Obj.GetIDList((uint)KNOWN_FOLDER_FLAG.KF_NO_FLAGS, out pidlPtr); } catch (ArgumentException) { Console.WriteLine("ArgumentException '{0}'", path); return(IntPtr.Zero); } return(pidlPtr); } } } using (var desktopFolder = new ShellFolderDesktop()) { SFGAO pdwAttributes = 0; uint pchEaten = 0; if (desktopFolder.Obj.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, path, ref pchEaten, out pidlPtr, ref pdwAttributes) == (uint)HRESULT.S_OK) { return(pidlPtr); } } return(IntPtr.Zero); }
public void GetPidlForDirectoryPath() { // Get the default drive's path var drive = new DirectoryInfo(Environment.SystemDirectory).Root.Name; Assert.IsFalse(string.IsNullOrEmpty(drive)); // enumerate on root directores of the default drive and // verify correct pidl <-> path reprentation for each directory foreach (var originalPath in Directory.EnumerateDirectories(drive)) { IntPtr pidl = default(IntPtr); try { pidl = PidlManager.GetPIDLFromPath(originalPath); Assert.IsFalse(pidl == default(IntPtr)); string logicalPath = PidlManager.GetPathFromPIDL(pidl); string physStorePath = PidlManager.GetPathFromPIDL(pidl, TypOfPath.PhysicalStoragePath); // The logical path of a special path '::{...}' will differ from // its physical representation 'C:\Windows' // Otherwise, both repesentations should be equal for non-special folders if (ShellHelpers.IsSpecialPath(logicalPath) == ShellHelpers.SpecialPath.IsSpecialPath) { Assert.IsFalse(string.Equals(logicalPath, physStorePath)); } else { Assert.IsTrue(string.Equals(logicalPath, physStorePath)); } ////Console.WriteLine("Path Retrieval via PIDL: '{0}' -> Logical Path: '{1}', Physical Path: '{2}'", //// originalPath, logicalPath, physStorePath); } finally { pidl = PidlManager.ILFree(pidl); } } }
/// <summary> /// Class constructor from strings that are commonly exposed by /// <see cref="IShellFolder2"/> interfaces. Constructing from these /// items can speed up enumeration since we do not need to revisit /// each items <see cref="IShellFolder2"/> interfaces. /// </summary> /// <param name="parseName"></param> /// <param name="name"></param> /// <param name="labelName"></param> /// <returns></returns> internal static BrowseItemFromPath InitItem(string parseName, string name, string labelName) { //// bool hasPIDL = false; //// IdList parentIdList = null; //// IdList relativeChildIdList = null; string path = parseName; string normPath = null, SpecialPathId = null; ShellHelpers.SpecialPath isSpecialID = ShellHelpers.IsSpecialPath(path); if (isSpecialID == ShellHelpers.SpecialPath.None) { normPath = parseName; } else { SpecialPathId = path; } //// hasPIDL = PidlManager.GetParentIdListFromPath(path, out parentIdList, out relativeChildIdList); //// if (hasPIDL == false) // return references that cannot resolve with a PIDL //// { //// var ret = new BrowseItemFromPath2(path, path); //// ret.Name = path; //// return ret; //// } //// IdList fullIdList = null; //// //// // Get the IShellFolder2 Interface for the original path item... //// // We are asked to build the desktop root item here... //// if ((parentIdList == null && relativeChildIdList == null) == false) //// fullIdList = PidlManager.CombineParentChild(parentIdList, relativeChildIdList); return(new BrowseItemFromPath(path, parseName, name, labelName, SpecialPathId, normPath //// ,parentIdList, relativeChildIdList )); }
//// public void LoadProperties() //// { //// if (_KnownFolderIsInitialized == false) //// { //// _KnownFolderIsInitialized = true; //// KnownFolder = LoadKnownFolder(); //// } //// //// if (_ItemTypeIsInitialized == false) //// { //// _ItemTypeIsInitialized = true; //// ItemType = LoadItemType(); //// } //// //// if (_IconResourceIdInitialized == false) //// { //// _IconResourceIdInitialized = true; //// _IconResourceId = LoadIconResourceId(); //// } //// } /// <summary> /// Initializes the items type flags and path properties. /// </summary> /// <param name="path">Is either a path reference a la 'C:' or a /// special folder path reference a la '::{...}' <seealso cref="KF_IID"/> /// for more details.</param> /// <returns>Returns a simple pojo type object to initialize /// the calling object members.</returns> internal static BrowseItemFromPath InitItem(string path) { if (string.IsNullOrEmpty(path) == true) // return unknown references { var ret = new BrowseItemFromPath(path, path); ret.Name = path; return(ret); } if (path.Length == 38) { try { Guid theGuid; if (Guid.TryParse(path, out theGuid) == true) { path = KF_IID.IID_Prefix + path; } } catch { // Catching errors just in case ... } } // Return item for root desktop item if (string.Compare(path, KF_IID.ID_ROOT_Desktop, true) == 0) { return(InitDesktopRootItem()); } ShellHelpers.SpecialPath isSpecialID = ShellHelpers.IsSpecialPath(path); string normPath = null, SpecialPathId = null; bool hasPIDL = false; IdList parentIdList, relativeChildIdList; if (isSpecialID == ShellHelpers.SpecialPath.IsSpecialPath) { SpecialPathId = path; hasPIDL = PidlManager.GetParentIdListFromPath(path, out parentIdList, out relativeChildIdList); } else { normPath = Browser.NormalizePath(path); hasPIDL = PidlManager.GetParentIdListFromPath(normPath, out parentIdList, out relativeChildIdList); } if (hasPIDL == false) // return references that cannot resolve with a PIDL { var ret = new BrowseItemFromPath(path, path); ret.Name = path; return(ret); } string parseName = normPath; string name = normPath; string labelName = null; IdList fullIdList = null; // Get the IShellFolder2 Interface for the original path item... IntPtr fullPidlPtr = default(IntPtr); IntPtr ptrShellFolder = default(IntPtr); IntPtr parentPIDLPtr = default(IntPtr); IntPtr relativeChildPIDLPtr = default(IntPtr); try { // We are asked to build the desktop root item here... if (parentIdList == null && relativeChildIdList == null) { using (var shellFolder = new ShellFolderDesktop()) { parseName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_FORPARSING); name = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING); labelName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_NORMAL); } } else { fullIdList = PidlManager.CombineParentChild(parentIdList, relativeChildIdList); fullPidlPtr = PidlManager.IdListToPidl(fullIdList); if (fullPidlPtr == default(IntPtr)) { return(null); } HRESULT hr = HRESULT.False; if (fullIdList.Size == 1) // Is this item directly under the desktop root? { hr = NativeMethods.SHGetDesktopFolder(out ptrShellFolder); if (hr != HRESULT.S_OK) { return(null); } using (var shellFolder = new ShellFolder(ptrShellFolder)) { parseName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_FORPARSING); name = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING); labelName = shellFolder.GetShellFolderName(fullPidlPtr, SHGDNF.SHGDN_NORMAL); } } else { parentPIDLPtr = PidlManager.IdListToPidl(parentIdList); relativeChildPIDLPtr = PidlManager.IdListToPidl(relativeChildIdList); using (var desktopFolder = new ShellFolderDesktop()) { hr = desktopFolder.Obj.BindToObject(parentPIDLPtr, IntPtr.Zero, typeof(IShellFolder2).GUID, out ptrShellFolder); } if (hr != HRESULT.S_OK) { return(null); } // This item is not directly under the Desktop root using (var shellFolder = new ShellFolder(ptrShellFolder)) { parseName = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_FORPARSING); name = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_INFOLDER | SHGDNF.SHGDN_FOREDITING); labelName = shellFolder.GetShellFolderName(relativeChildPIDLPtr, SHGDNF.SHGDN_NORMAL); } } } if (ShellHelpers.IsSpecialPath(parseName) == ShellHelpers.SpecialPath.None) { normPath = parseName; } return(new BrowseItemFromPath(path, parseName, name, labelName, SpecialPathId, normPath //// ,parentIdList, relativeChildIdList )); } finally { PidlManager.ILFree(parentPIDLPtr); PidlManager.ILFree(relativeChildPIDLPtr); if (fullPidlPtr != default(IntPtr)) { NativeMethods.ILFree(fullPidlPtr); } } }
/// <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); }