[源码下载]

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

作者:webabcd

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

  • 获取文件的属性
  • 修改文件的属性
  • 获取文件的缩略图

示例
1、演示如何获取文件的属性,修改文件的属性
FileSystem/FileProperties.xaml

<Page
x:Class="Windows10.FileSystem.FileProperties"
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" /> <Button Name="btnModifyProperty" Content="修改文件的属性" Margin="5" Click="btnModifyProperty_Click" /> <TextBlock Name="lblMsg" Margin="5" /> </StackPanel>
</Grid>
</Page>

FileSystem/FileProperties.xaml.cs

/*
* 演示如何获取文件的属性,修改文件的属性
*
* StorageFile - 文件操作类
* 直接通过调用 Name, Path, DisplayName, DisplayType, FolderRelativeId, Provider, DateCreated, Attributes, ContentType, FileType, IsAvailable 获取相关属性,详见文档
* GetBasicPropertiesAsync() - 返回一个 BasicProperties 类型的对象
* Properties - 返回一个 StorageItemContentProperties 类型的对象
*
* BasicProperties
* 可以获取的数据有 Size, DateModified, ItemDate
*
* StorageItemContentProperties
* 通过 GetImagePropertiesAsync() 获取图片属性,详见文档
* 通过 GetVideoPropertiesAsync() 获取视频属性,详见文档
* 通过 GetMusicPropertiesAsync() 获取音频属性,详见文档
* 通过 GetDocumentPropertiesAsync() 获取文档属性,详见文档
* 通过调用 RetrievePropertiesAsync() 方法来获取指定的属性,然后可以调用 SavePropertiesAsync() 方法来保存修改后的属性,详见下面的示例
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.FileSystem
{
public sealed partial class FileProperties : Page
{
public FileProperties()
{
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); // 显示文件的各种属性
ShowProperties1(storageFile);
await ShowProperties2(storageFile);
await ShowProperties3(storageFile);
await ShowProperties4(storageFile);
} // 通过 StorageFile 获取文件的属性
private void ShowProperties1(StorageFile storageFile)
{
lblMsg.Text = "Name:" + storageFile.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Path:" + storageFile.Path;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DisplayName:" + storageFile.DisplayName;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DisplayType:" + storageFile.DisplayType;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FolderRelativeId:" + storageFile.FolderRelativeId;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Provider:" + storageFile.Provider;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DateCreated:" + storageFile.DateCreated;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "Attributes:" + storageFile.Attributes; // 返回一个 FileAttributes 类型的枚举(FlagsAttribute),可以从中获知文件是否是 ReadOnly 之类的信息
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ContentType:" + storageFile.ContentType;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "FileType:" + storageFile.FileType;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "IsAvailable:" + storageFile.IsAvailable;
lblMsg.Text += Environment.NewLine;
} // 通过 StorageFile.GetBasicPropertiesAsync() 获取文件的属性
private async Task ShowProperties2(StorageFile storageFile)
{
BasicProperties basicProperties = await storageFile.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(StorageFile storageFile)
{
/*
* 获取文件的其它各种属性
* 详细的属性列表请参见结尾处的“附录一: 属性列表”或者参见: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");
propertiesName.Add("System.FileAttributes"); StorageItemContentProperties storageItemContentProperties = storageFile.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;
lblMsg.Text += "System.FileAttributes:" + extraProperties["System.FileAttributes"];
lblMsg.Text += Environment.NewLine;
} // 通过 StorageFolder.Properties 的 GetImagePropertiesAsync(), GetVideoPropertiesAsync(), GetMusicPropertiesAsync(), GetDocumentPropertiesAsync() 方法获取文件的属性
private async Task ShowProperties4(StorageFile storageFile)
{
StorageItemContentProperties storageItemContentProperties = storageFile.Properties;
ImageProperties imageProperties = await storageItemContentProperties.GetImagePropertiesAsync(); // 图片属性
VideoProperties videoProperties = await storageItemContentProperties.GetVideoPropertiesAsync(); // 视频属性
MusicProperties musicProperties = await storageItemContentProperties.GetMusicPropertiesAsync(); // 音频属性
DocumentProperties documentProperties = await storageItemContentProperties.GetDocumentPropertiesAsync(); // 文档属性 lblMsg.Text += "image width:" + imageProperties.Width;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "image height:" + imageProperties.Height;
lblMsg.Text += Environment.NewLine;
} // 修改文件的属性
private async void btnModifyProperty_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// 用户选中的文件
string fileName = (string)listBox.SelectedItem;
StorageFolder picturesFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.PicturesLibrary);
StorageFile storageFile = await picturesFolder.GetFileAsync(fileName); // 在 System.FileAttributes 中保存有文件是否是“只读”的信息
string[] retrieveList = new string[] { "System.FileAttributes" };
StorageItemContentProperties storageItemContentProperties = storageFile.Properties;
IDictionary<string, object> extraProperties = await storageItemContentProperties.RetrievePropertiesAsync(retrieveList); uint FILE_ATTRIBUTES_READONLY = ;
if (extraProperties != null && extraProperties.ContainsKey("System.FileAttributes"))
{
// 切换文件的只读属性
uint temp = (UInt32)extraProperties["System.FileAttributes"] ^ FILE_ATTRIBUTES_READONLY; // 设置文件的只读属性为 true
// uint temp = (UInt32)extraProperties["System.FileAttributes"] | 1; // 设置文件的只读属性为 false
// uint temp = (UInt32)extraProperties["System.FileAttributes"] & 0; extraProperties["System.FileAttributes"] = temp;
}
else
{
// 设置文件的只读属性为 true
extraProperties = new PropertySet();
extraProperties.Add("System.FileAttributes", FILE_ATTRIBUTES_READONLY);
} // 保存修改后的属性(用这种方法可以修改部分属性,大部分属性都是无权限修改的)
await storageFile.Properties.SavePropertiesAsync(extraProperties);
}
}
} /*
----------------------------------------------------------------------
附录一: 属性列表 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 = ;
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
[源码下载]

FilePropertiesFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xamlFileSystem/FileProperties.xaml

背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图的更多相关文章

  1. 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

    [源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...

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

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

  3. 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件

    [源码下载] 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件 作者:webabcd 介绍背水一战 Windows 10 之 ...

  4. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  5. 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件

    [源码下载] 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 创建文件夹,重命名文件夹,删除文件夹, ...

  6. 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引

    [源码下载] 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引 作者:webabcd 介绍背水一战 Windows 10 之 文件系 ...

  7. 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

    [源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...

  8. 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性

    [源码下载] 背水一战 Windows 10 (72) - 控件(控件基类): UIElement - UIElement 的位置, UIElement 的布局, UIElement 的其他特性 作者 ...

  9. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

随机推荐

  1. 析构方法(__del__)

    析构方法,当对象在内存中被释放时(也就是实例执行完了,实例的内存就会自动释放,这时候就会触发),自动触发执行. 当程序结束时,python只会回收自己的内存空间,即用户态内存,而操作系统的资源则没有被 ...

  2. 创建Flask实例对象时的参数和app.run()中的参数

    app=Flask(name,static_folder=“static”,static_url_path="/aaa",template_folder=“templates”) ...

  3. 安装和激活Office 2019

    有条件请支持正版!相比费尽力气找一个可能不太安全的激活工具,直接买随时随地更新的Office 365确实是最好的办法.暂时没有经济实力的,可以看看这篇文章.下载OTP工具 首先到Office Tool ...

  4. Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的,其内部是怎么实现的

    其实也就是通过接口名与mapper的id绑定在一起(即相同),通过SQL去写实现类,返回数据.

  5. Spring中AOP主要用来做什么。Spring注入bean的方式。什么是IOC,什么是依赖注入

    Spring中主要用到的设计模式有工厂模式和代理模式. IOC:Inversion of Control控制反转,也叫依赖注入,通过 sessionfactory 去注入实例:IOC就是一个生产和管理 ...

  6. python中configpraser模块

    configparser   模块 解析配置文件模块 什么是配置文件? 用于编写程序的配置信息的文件 什么是配置信息? 为了提高程序的扩展性 #configparser模块的使用 #首先我们需要知道配 ...

  7. IDEA 工具从Json自动生成JavaBean

    1.先安装GsonFormat插件:File-->Setting-->Plugins-->GsonFormat-->OK 2.new 一个新的Class空文件,然后 Alt+I ...

  8. python获取文件夹的大小(即取出所有文件计算大小)

    import os path = r'/Users/authurchen/PycharmProjects/Demo' # print(os.listdir(path)) ls = os.listdir ...

  9. zabbix 3.4 直接 发现端口并作存活监控(带服务名)

    客户端配置 1.脚本 [root@es1 home]# cat /home/port_service.sh #!/bin/bash#by Mr.lu#su rootportarray=(`sudo - ...

  10. python argparse(参数解析)模块学习(二)

    转载自:http://www.cnblogs.com/fireflow/p/4841389.html(我去..没转载功能,ctrl + c 和 ctrl + v 得来的,格式有点问题,可去原版看看) ...