背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图

简介: 原文:背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图[源码下载] 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图 作者:webabcd介绍背水一战 Windows 10 之 文件系统 获取文...
原文: 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图

[源码下载]


背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图



作者:webabcd


介绍
背水一战 Windows 10 之 文件系统

  • 获取文件夹的属性
  • 获取文件夹的缩略图



示例
1、演示如何获取文件夹的属性
FileSystem/FolderProperties.xaml

<Page
    x:Class="Windows10.FileSystem.FolderProperties"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.FileSystem"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <ListBox Name="listBox" Width="400" Height="200" HorizontalAlignment="Left" Margin="5" SelectionChanged="listBox_SelectionChanged" />

            <TextBlock Name="lblMsg" Margin="5" />
            
        </StackPanel>
    </Grid>
</Page>

FileSystem/FolderProperties.xaml.cs

/*
 * 演示如何获取文件夹的属性
 * 
 * StorageFolder - 文件夹操作类
 *     直接通过调用 Name, Path, DisplayName, DisplayType, FolderRelativeId, Provider, DateCreated, Attributes 获取相关属性,详见文档
 *     GetBasicPropertiesAsync() - 返回一个 BasicProperties 类型的对象 
 *     Properties - 返回一个 StorageItemContentProperties 类型的对象
 *
 * BasicProperties
 *     可以获取的数据有 Size, DateModified, ItemDate
 * 
 * StorageItemContentProperties
 *     通过调用 RetrievePropertiesAsync() 方法来获取指定的属性,详见下面的示例
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace Windows10.FileSystem
{
    public sealed partial class FolderProperties : Page
    {
        public FolderProperties()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // 获取“图片库”目录下的所有根文件夹
            StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);
            IReadOnlyList<StorageFolder> folderList = await picturesFolder.GetFoldersAsync();
            listBox.ItemsSource = folderList.Select(p => p.Name).ToList();

            base.OnNavigatedTo(e);
        }

        private async void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 用户选中的文件夹
            string folderName = (string)listBox.SelectedItem;
            StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);
            StorageFolder storageFolder = await picturesFolder.GetFolderAsync(folderName);

            // 显示文件夹的各种属性
            ShowProperties1(storageFolder);
            await ShowProperties2(storageFolder);
            await ShowProperties3(storageFolder);
        }

        // 通过 StorageFolder 获取文件夹的属性
        private void ShowProperties1(StorageFolder storageFolder)
        {
            lblMsg.Text = "Name:" + storageFolder.Name;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "Path:" + storageFolder.Path;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "DisplayName:" + storageFolder.DisplayName;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "DisplayType:" + storageFolder.DisplayType;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "FolderRelativeId:" + storageFolder.FolderRelativeId;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "Provider:" + storageFolder.Provider;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "DateCreated:" + storageFolder.DateCreated;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "Attributes:" + storageFolder.Attributes; // 返回一个 FileAttributes 类型的枚举(FlagsAttribute),可以从中获知文件夹是否是 ReadOnly 之类的信息
            lblMsg.Text += Environment.NewLine;
        }

        // 通过 StorageFolder.GetBasicPropertiesAsync() 获取文件夹的属性
        private async Task ShowProperties2(StorageFolder storageFolder)
        {
            BasicProperties basicProperties = await storageFolder.GetBasicPropertiesAsync();
            lblMsg.Text += "Size:" + basicProperties.Size;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "DateModified:" + basicProperties.DateModified;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "ItemDate:" + basicProperties.ItemDate;
            lblMsg.Text += Environment.NewLine;
        }

        // 通过 StorageFolder.Properties 的 RetrievePropertiesAsync() 方法获取文件夹的属性
        private async Task ShowProperties3(StorageFolder storageFolder)
        {
            /*
             * 获取文件夹的其它各种属性
             * 详细的属性列表请参见结尾处的“附录一: 属性列表”或者参见:http://msdn.microsoft.com/en-us/library/windows/desktop/ff521735(v=vs.85).aspx
             */
            List<string> propertiesName = new List<string>();
            propertiesName.Add("System.DateAccessed");
            propertiesName.Add("System.DateCreated");
            propertiesName.Add("System.FileOwner");

            StorageItemContentProperties storageItemContentProperties = storageFolder.Properties;
            IDictionary<string, object> extraProperties = await storageItemContentProperties.RetrievePropertiesAsync(propertiesName);

            lblMsg.Text += "System.DateAccessed:" + extraProperties["System.DateAccessed"];
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "System.DateCreated:" + extraProperties["System.DateCreated"];
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "System.FileOwner:" + extraProperties["System.FileOwner"];
            lblMsg.Text += Environment.NewLine;
        }
    }
}


/*
----------------------------------------------------------------------
附录一: 属性列表

System.AcquisitionID
System.ApplicationName
System.Author
System.Capacity
System.Category
System.Comment
System.Company
System.ComputerName
System.ContainedItems
System.ContentStatus
System.ContentType
System.Copyright
System.DataObjectFormat
System.DateAccessed
System.DateAcquired
System.DateArchived
System.DateCompleted
System.DateCreated
System.DateImported
System.DateModified
System.DefaultSaveLocationIconDisplay
System.DueDate
System.EndDate
System.FileAllocationSize
System.FileAttributes
System.FileCount
System.FileDescription
System.FileExtension
System.FileFRN
System.FileName
System.FileOwner
System.FileVersion
System.FindData
System.FlagColor
System.FlagColorText
System.FlagStatus
System.FlagStatusText
System.FreeSpace
System.FullText
System.Identity
System.Identity.Blob
System.Identity.DisplayName
System.Identity.IsMeIdentity
System.Identity.PrimaryEmailAddress
System.Identity.ProviderID
System.Identity.UniqueID
System.Identity.UserName
System.IdentityProvider.Name
System.IdentityProvider.Picture
System.ImageParsingName
System.Importance
System.ImportanceText
System.IsAttachment
System.IsDefaultNonOwnerSaveLocation
System.IsDefaultSaveLocation
System.IsDeleted
System.IsEncrypted
System.IsFlagged
System.IsFlaggedComplete
System.IsIncomplete
System.IsLocationSupported
System.IsPinnedToNameSpaceTree
System.IsRead
System.IsSearchOnlyItem
System.IsSendToTarget
System.IsShared
System.ItemAuthors
System.ItemClassType
System.ItemDate
System.ItemFolderNameDisplay
System.ItemFolderPathDisplay
System.ItemFolderPathDisplayNarrow
System.ItemName
System.ItemNameDisplay
System.ItemNamePrefix
System.ItemParticipants
System.ItemPathDisplay
System.ItemPathDisplayNarrow
System.ItemType
System.ItemTypeText
System.ItemUrl
System.Keywords
System.Kind
System.KindText
System.Language
System.LayoutPattern.ContentViewModeForBrowse
System.LayoutPattern.ContentViewModeForSearch
System.LibraryLocationsCount
System.MileageInformation
System.MIMEType
System.Null
System.OfflineAvailability
System.OfflineStatus
System.OriginalFileName
System.OwnerSID
System.ParentalRating
System.ParentalRatingReason
System.ParentalRatingsOrganization
System.ParsingBindContext
System.ParsingName
System.ParsingPath
System.PerceivedType
System.PercentFull
System.Priority
System.PriorityText
System.Project
System.ProviderItemID
System.Rating
System.RatingText
System.Sensitivity
System.SensitivityText
System.SFGAOFlags
System.SharedWith
System.ShareUserRating
System.SharingStatus
System.Shell.OmitFromView
System.SimpleRating
System.Size
System.SoftwareUsed
System.SourceItem
System.StartDate
System.Status
System.StatusBarSelectedItemCount
System.StatusBarViewItemCount
System.Subject
System.Thumbnail
System.ThumbnailCacheId
System.ThumbnailStream
System.Title
System.TotalFileSize
System.Trademarks
----------------------------------------------------------------------
*/


2、演示如何获取文件的缩略图
FileSystem/FileThumbnail.xaml

<Page
    x:Class="Windows10.FileSystem.FileThumbnail"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.FileSystem"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />

            <ListBox Name="listBox" Width="400" Height="200" HorizontalAlignment="Left" Margin="5" SelectionChanged="listBox_SelectionChanged" />

            <Image Name="imageThumbnail" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5" />

        </StackPanel>
    </Grid>
</Page>

FileSystem/FileThumbnail.xaml.cs

/*
 * 演示如何获取文件的缩略图
 * 
 * StorageFile - 文件操作类。与获取文件缩略图相关的接口如下
 *     public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode);
 *     public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize);
 *     public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options);
 *         ThumbnailMode mode - 用于描述缩略图的目的,以使系统确定缩略图图像的调整方式(就用 SingleItem 即可)
 *         uint requestedSize - 期望尺寸的最长边长的大小
 *         ThumbnailOptions options - 检索和调整缩略图的行为(默认值:UseCurrentScale)
 *         
 * StorageItemThumbnail - 缩略图(实现了 IRandomAccessStream 接口,可以直接转换为 BitmapImage 对象)
 *     OriginalWidth - 缩略图的宽
 *     OriginalHeight - 缩略图的高
 *     Size - 缩略图的大小(单位:字节)
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace Windows10.FileSystem
{
    public sealed partial class FileThumbnail : Page
    {
        public FileThumbnail()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // 获取“图片库”目录下的所有根文件
            StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);
            IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync();
            listBox.ItemsSource = fileList.Select(p => p.Name).ToList();

            base.OnNavigatedTo(e);
        }

        private async void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 用户选中的文件
            string fileName = (string)listBox.SelectedItem;
            StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);
            StorageFile storageFile = await picturesFolder.GetFileAsync(fileName);

            // 显示文件的缩略图
            await ShowThumbnail(storageFile);
        }

        private async Task ShowThumbnail(StorageFile storageFile)
        {
            // 如果要获取文件的缩略图,就指定为 ThumbnailMode.SingleItem 即可
            ThumbnailMode thumbnailMode = ThumbnailMode.SingleItem;
            uint requestedSize = 200;
            ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale;

            using (StorageItemThumbnail thumbnail = await storageFile.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions))
            {
                if (thumbnail != null)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(thumbnail);
                    imageThumbnail.Source = bitmapImage;

                    lblMsg.Text = $"thumbnail1 requestedSize:{requestedSize}, returnedSize:{thumbnail.OriginalWidth}x{thumbnail.OriginalHeight}, size:{thumbnail.Size}";
                }
            }
        }
    }
}



OK
[源码下载]

目录
相关文章
|
5月前
|
存储 安全 Shell
windows 操作系统隐藏文件夹 .ssh 的作用
windows 操作系统隐藏文件夹 .ssh 的作用
|
6天前
|
Linux Windows
Windows系统批量创建文件夹的技巧
Windows系统批量创建文件夹的技巧
14 1
|
1月前
|
Windows
windows 文件夹视图全局生效
【8月更文挑战第31天】在 Windows 中,要使文件夹视图设置全局生效,请先在一个文件夹中设置视图模式和排序方式等,然后点击“查看”选项卡中的“选项”按钮,打开“文件夹选项”,切换到“查看”选项卡,点击“应用到文件夹”按钮以确认设置。这样,大多数文件夹将采用相同视图。不过,部分特殊文件夹可能不遵循此设置。
|
2月前
|
Windows
windows 文件夹视图全局生效
【8月更文挑战第20天】在Windows中实现文件夹视图全局设置:首先调整任一文件夹的视图样式,如选择“大图标”或“详细信息”。接着设置排序和分组选项。随后,在该文件夹的“查看”选项卡中点击“选项”,在“文件夹选项”的“查看”标签下点击“应用到文件夹”。确认后,所有文件夹将采用相同视图。注意:特定文件夹可能不受此设置影响。
|
2月前
|
API C# Shell
WPF与Windows Shell完美融合:深入解析文件系统操作技巧——从基本文件管理到高级Shell功能调用,全面掌握WPF中的文件处理艺术
【8月更文挑战第31天】Windows Presentation Foundation (WPF) 是 .NET Framework 的关键组件,用于构建 Windows 桌面应用程序。WPF 提供了丰富的功能来创建美观且功能强大的用户界面。本文通过问题解答的形式,探讨了如何在 WPF 应用中集成 Windows Shell 功能,并通过具体示例代码展示了文件系统的操作方法,包括列出目录下的所有文件、创建和删除文件、移动和复制文件以及打开文件夹或文件等。
45 0
|
2月前
|
开发框架 .NET API
Windows Server 2022 安装IIS 报错 访问临时文件夹 C:\WINDOWS\TEMP\3C 读取/写入权限 错误: 0x80070005
Windows Server 2022 安装IIS 报错 访问临时文件夹 C:\WINDOWS\TEMP\3C 读取/写入权限 错误: 0x80070005
86 0
|
5月前
|
存储 安全 Shell
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 known_hosts 文件的作用
|
5月前
|
安全 Shell 网络安全
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
windows 系统 c 盘 .ssh 文件夹里的 id_rsa 文件的作用
|
4月前
|
Windows
windows系统vbs脚本 提取文件夹中的所有文件名
windows系统vbs脚本 提取文件夹中的所有文件名
39 0
|
5月前
|
存储 安全 搜索推荐
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
下一篇
无影云桌面