/// <summary> /// Class constructor fron internal structure. /// </summary> /// <param name="knownFolderNative"></param> /// <param name="nativeFolderDefinition"></param> public KnownFolderProperties(IKnownFolderNative knownFolderNative, NativeFolderDefinition nativeFolderDefinition) : this() { try { Init(knownFolderNative, nativeFolderDefinition); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
/// <summary> /// Populates an object that contains a known folder's properties. /// </summary> public static IKnownFolderProperties GetFolderProperties(IKnownFolderNative knownFolderNative) { Debug.Assert(knownFolderNative != null); NativeFolderDefinition nativeFolderDefinition = default(NativeFolderDefinition); try { knownFolderNative.GetFolderDefinition(out nativeFolderDefinition); KnownFolderProperties knownFolderProperties = new KnownFolderProperties(knownFolderNative, nativeFolderDefinition); // Error handling - Empty Pidl indicates non-existing local folder // Let's not worry about non-exisitng local 'known' folders if (knownFolderProperties.PidlIdList.Size == 0 && knownFolderProperties.FolderId != new Guid(KF_ID.ID_FOLDERID_Desktop)) { return(default(KnownFolderProperties)); } return(knownFolderProperties); } catch { return(default(KnownFolderProperties)); } finally { // Clean up memory. ////FolderCategory category Marshal.FreeCoTaskMem(nativeFolderDefinition.name); Marshal.FreeCoTaskMem(nativeFolderDefinition.description); ////Guid parentId; Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath); Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName); Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip); Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName); Marshal.FreeCoTaskMem(nativeFolderDefinition.icon); Marshal.FreeCoTaskMem(nativeFolderDefinition.security); ////UInt32 attributes; ////DefinitionOptions definitionOptions; ////Guid folderTypeId; } }
public virtual extern void RegisterFolder( [In, MarshalAs(UnmanagedType.LPStruct)] Guid knownFolderGuid, [In] ref NativeFolderDefinition knownFolderDefinition);
private void Init(IKnownFolderNative knownFolderNative, NativeFolderDefinition nativeFolderDefinition) { this.Name = Marshal.PtrToStringUni(nativeFolderDefinition.name); this.Category = nativeFolderDefinition.category; this.CanonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name); this.Description = Marshal.PtrToStringUni(nativeFolderDefinition.description); this.ParentId = nativeFolderDefinition.parentId; this.RelativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath); this.ParsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName); this.TooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip); this.LocalizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName); this.IconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon); this.Security = Marshal.PtrToStringUni(nativeFolderDefinition.security); this.FileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes; this.DefinitionOptions = nativeFolderDefinition.definitionOptions; this.FolderTypeId = nativeFolderDefinition.folderTypeId; ////knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId); this.Redirection = knownFolderNative.GetRedirectionCapabilities(); // Turn tooltip, localized name and icon resource IDs // into the actual resources. this.Tooltip = GetStringResource(this.TooltipResourceId); this.LocalizedName = GetStringResource(this.LocalizedNameResourceId); this.FolderId = knownFolderNative.GetId(); bool pathExists = false; this.IsExistsInFileSystem = false; this.IsPathExists = false; this.PidlIdList = null; using (var kfObj = new KnownFolderNative(knownFolderNative)) { if (kfObj != null) { try { bool?isExists = kfObj.IsFileSystem(); if (isExists != null) { if (isExists == true) { this.IsExistsInFileSystem = true; } this.Path = KnownFolderHelper.GetPath(out pathExists, knownFolderNative); this.IsPathExists = pathExists; } } catch { // Catch this just in case } try { this.PidlIdList = kfObj.KnownFolderToIdList(); } catch { // Catch this just in case } } } // Load Icon ResourceId from Icon Resource helper (if not already present) if (IsIconResourceIdValid(IconResourceId) == false && PidlIdList != null) { if (PidlIdList.Size == 0 && this.FolderId == new Guid(KF_ID.ID_FOLDERID_Desktop)) { IconResourceId = IconHelper.FromPidl(PidlIdList, true, false); } else { if (PidlIdList.Size != 0) { IconResourceId = IconHelper.FromPidl(PidlIdList, true, false); } } } }