背水一戰 Windows 10 (86) - 文件系統: 獲取文件夾的屬性, 獲取文件夾的縮略圖

来源:https://www.cnblogs.com/webabcd/archive/2018/01/15/8286680.html
-Advertisement-
Play Games

背水一戰 Windows 10 之 文件系統: 獲取文件夾的屬性, 獲取文件夾的縮略圖 ...


[源碼下載]


背水一戰 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
[源碼下載]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • (一)指針數組 指針數組就是每一個元素存放一個地址,相當於一個指針變數。如:int *p[4]指針數組比較適合用來指向若幹字元串,使得處理字元串更加靈活。例如,現在要將若幹字元串按字母順序由小到大輸出 通過上例子,試比較if(strcmp(name[k],name[j])>0)和if(strcmp( ...
  • 上次通過eclipse在控制台輸出了hello world,是不是有點小激動啊,今天接著介紹Java基礎知識。 一、Java註釋 1、Java註釋語句不會被編譯器運行,不用擔心代碼因為許多註釋語句顯得臃腫而影響程式運行速度。 2、Java註釋有三種寫法。 一是雙斜杠 // 。需要註掉哪一行就添加到哪 ...
  • 1.python模塊:標準庫和第三方庫,第三方庫需要下載安裝2.模塊sys:命令 功能 sys.stdin 標準輸入流sys.stdout 標準輸出流sys.stderr 標準錯誤流 sys.argv[value] 接收命令行的參數。例如,windows下的命令行cmd裡面的參數。其中,argv[0 ...
  • 1. Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application de ...
  • Using mac os python3.6 to connect ssl will occur urllib.error.URLError. It requires a post-install step, which installs the certifi package of certifi ...
  • 一、Listener監聽器 Javaweb開發中的監聽器,是用於監聽web常見對象 HttpServletRequest HttpSession ServletContext 監聽它們的創建與銷毀、屬性變化 以及session綁定javaBean 1、監聽機制 事件 就是一個事情 事件源 產生這個事 ...
  • 才開始《Spring源碼深度解析》就碰到了問題,按照書上的步驟從github上下載了源碼,然後導入項目後,缺少spring-cglib-repack-3.2.5.jar和spring-objenesis-repack-2.6.jar這兩個jar包。 網上很多解決辦法都是從spring-core中解壓 ...
  • 最近老是聽說協程很火,心也很癢癢想知道這到底是個什麼東西,今天就花功夫看了看Boost庫里的Coroutine。誰不曾想Boost庫這麼難搞,等到要寫代碼時編譯出錯了。其實這也不能怪Boost,大部分Boost庫都是以頭文件的形式提供的,直接include就可以了。但是Coroutine這個東西用了 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...