/// <summary> /// Populates a structure that contains /// this known folder's properties. /// </summary> private void GetFolderProperties(IKnownFolderNative knownFolderNative) { Debug.Assert(knownFolderNative != null); KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition; knownFolderNative.GetFolderDefinition(out nativeFolderDefinition); try { knownFolderProperties.category = nativeFolderDefinition.category; knownFolderProperties.canonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name); knownFolderProperties.description = Marshal.PtrToStringUni(nativeFolderDefinition.description); knownFolderProperties.parentId = nativeFolderDefinition.parentId; knownFolderProperties.relativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath); knownFolderProperties.parsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName); knownFolderProperties.tooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip); knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName); knownFolderProperties.iconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon); knownFolderProperties.security = Marshal.PtrToStringUni(nativeFolderDefinition.security); knownFolderProperties.fileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes; knownFolderProperties.definitionOptions = nativeFolderDefinition.definitionOptions; knownFolderProperties.folderTypeId = nativeFolderDefinition.folderTypeId; knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId); bool pathExists; knownFolderProperties.path = GetPath(out pathExists, knownFolderNative); knownFolderProperties.pathExists = pathExists; knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities(); // Turn tooltip, localized name and icon resource IDs // into the actual resources. knownFolderProperties.tooltip = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId); knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId); knownFolderProperties.folderId = knownFolderNative.GetId(); } finally { // Clean up memory. Marshal.FreeCoTaskMem(nativeFolderDefinition.name); Marshal.FreeCoTaskMem(nativeFolderDefinition.description); Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath); Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName); Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip); Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName); Marshal.FreeCoTaskMem(nativeFolderDefinition.icon); Marshal.FreeCoTaskMem(nativeFolderDefinition.security); } }
/// <summary> /// Populates a structure that contains /// this known folder's properties. /// </summary> private void GetFolderProperties(IKnownFolderNative knownFolderNative) { Debug.Assert(knownFolderNative != null); NativeFolderDefinition nativeFolderDefinition; knownFolderNative.GetFolderDefinition(out nativeFolderDefinition); try { knownFolderProperties.category = nativeFolderDefinition.category; knownFolderProperties.canonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name); knownFolderProperties.description = Marshal.PtrToStringUni(nativeFolderDefinition.description); knownFolderProperties.parentId = nativeFolderDefinition.parentId; knownFolderProperties.relativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath); knownFolderProperties.parsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName); knownFolderProperties.tooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip); knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName); knownFolderProperties.iconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon); knownFolderProperties.security = Marshal.PtrToStringUni(nativeFolderDefinition.security); knownFolderProperties.fileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes; knownFolderProperties.definitionOptions = nativeFolderDefinition.definitionOptions; knownFolderProperties.folderTypeId = nativeFolderDefinition.folderTypeId; knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId); bool pathExists; knownFolderProperties.path = GetPath(out pathExists, knownFolderNative); knownFolderProperties.pathExists = pathExists; knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities(); // Turn tooltip, localized name and icon resource IDs // into the actual resources. //FIXME: //knownFolderProperties.tooltip = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId); //knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId); knownFolderProperties.folderId = knownFolderNative.GetId(); } finally { // Clean up memory. Marshal.FreeCoTaskMem(nativeFolderDefinition.name); Marshal.FreeCoTaskMem(nativeFolderDefinition.description); Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath); Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName); Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip); Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName); Marshal.FreeCoTaskMem(nativeFolderDefinition.icon); Marshal.FreeCoTaskMem(nativeFolderDefinition.security); } }
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); } } } }
/// <summary> /// Populates a structure that contains /// this known folder's properties. /// </summary> private void GetFolderProperties(IKnownFolderNative knownFolderNative) { Debug.Assert(knownFolderNative != null); KnownFoldersSafeNativeMethods.NativeFolderDefinition nativeFolderDefinition; knownFolderNative.GetFolderDefinition(out nativeFolderDefinition); try { knownFolderProperties.category = nativeFolderDefinition.category; knownFolderProperties.canonicalName = Marshal.PtrToStringUni(nativeFolderDefinition.name); knownFolderProperties.description = Marshal.PtrToStringUni(nativeFolderDefinition.description); knownFolderProperties.parentId = nativeFolderDefinition.parentId; knownFolderProperties.relativePath = Marshal.PtrToStringUni(nativeFolderDefinition.relativePath); knownFolderProperties.parsingName = Marshal.PtrToStringUni(nativeFolderDefinition.parsingName); knownFolderProperties.tooltipResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.tooltip); knownFolderProperties.localizedNameResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.localizedName); knownFolderProperties.iconResourceId = Marshal.PtrToStringUni(nativeFolderDefinition.icon); knownFolderProperties.security = Marshal.PtrToStringUni(nativeFolderDefinition.security); knownFolderProperties.fileAttributes = (System.IO.FileAttributes)nativeFolderDefinition.attributes; knownFolderProperties.definitionOptions = nativeFolderDefinition.definitionOptions; knownFolderProperties.folderTypeId = nativeFolderDefinition.folderTypeId; knownFolderProperties.folderType = FolderTypes.GetFolderType(knownFolderProperties.folderTypeId); bool pathExists; knownFolderProperties.path = GetPath(out pathExists, knownFolderNative); knownFolderProperties.pathExists = pathExists; knownFolderProperties.redirection = knownFolderNative.GetRedirectionCapabilities(); // Turn tooltip, localized name and icon resource IDs // into the actual resources. try { knownFolderProperties.tooltip = CoreHelpers.GetStringResource(knownFolderProperties.tooltipResourceId); } catch(Win32Exception) { // The resource ID isn't valid. This is most likely because there is no resource // defined for this KnownFolder. We should keep the default value (i.e. empty string) // and not throw an exception or set it to null. } try { knownFolderProperties.localizedName = CoreHelpers.GetStringResource(knownFolderProperties.localizedNameResourceId); } catch(Win32Exception) { // The resource ID isn't valid. This is most likely because there is no resource // defined for this KnownFolder. We should keep the default value (i.e. empty string) // and not throw an exception or set it to null. } knownFolderProperties.folderId = knownFolderNative.GetId(); } finally { // Clean up memory. Marshal.FreeCoTaskMem(nativeFolderDefinition.name); Marshal.FreeCoTaskMem(nativeFolderDefinition.description); Marshal.FreeCoTaskMem(nativeFolderDefinition.relativePath); Marshal.FreeCoTaskMem(nativeFolderDefinition.parsingName); Marshal.FreeCoTaskMem(nativeFolderDefinition.tooltip); Marshal.FreeCoTaskMem(nativeFolderDefinition.localizedName); Marshal.FreeCoTaskMem(nativeFolderDefinition.icon); Marshal.FreeCoTaskMem(nativeFolderDefinition.security); } }