【万里征程——Windows App开发】文件&数据——获取文件属性

简介:

这一节来看看获取文件属性吧,可以获取到文件名、类型、最近访问时间等等属性哦。

创建Button和TextBlock

下面这段代码呢,都很简单。

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button Width="200" Height="70" Name="btnGetProp" Content="获取文件属性" Click="btnGetProp_Click"/>            
            <TextBlock Name="tBlockProp" Margin="12" Width="480" FontSize="30"/>          
        </StackPanel>
    </Grid>
</Page>

在Click事件中,先获取到图片库,当然了,你可以获取其他地方,我电脑上的库文件中,就只有文档库和图片库有文件了。然后创建一个文件查询,最后将这些文件都赋给files。这里的var可谓是非常的强大啊。实例化一个StringBuilder对象来辅助输出信息那真是太完美不过了,我们在前面也常用到它。

var folder = KnownFolders.PicturesLibrary;
var fileQuery = folder.CreateFileQuery();
var files = await fileQuery.GetFilesAsync();
StringBuilder fileProperties = new StringBuilder();
for (int i = 0; i < files.Count; i++)
{
     StorageFile file = files[i];
     fileProperties.AppendLine("File name: " + file.Name);   
     fileProperties.AppendLine("File type: " + file.FileType);
     BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
     string fileSize = string.Format("{0:n0}", basicProperties.Size);
     fileProperties.AppendLine("File size: " + fileSize + " bytes");
     fileProperties.AppendLine("Date modified: " + basicProperties.DateModified);
     fileProperties.AppendLine(" ");
}
tBlockProp.Text = fileProperties.ToString();

这样一来就完成对Name、FileType、Size和DateModified属性的获取,但还有一类属性,则比较难以获取,它们就是“扩展属性”。

List<string> propertiesName = new List<string>();
propertiesName.Add("System.DateAccessed");
propertiesName.Add("System.FileOwner");
IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);
var propValue = extraProperties[dateAccessedProperty];
if (propValue != null)
     fileProperties.AppendLine("Date accessed: " + propValue);
propValue = extraProperties[fileOwnerProperty];
if (propValue != null)
     fileProperties.AppendLine("File owner: " + propValue);

最后将fileProperties传递给TextBlock即可。

tBlockProp.Text = fileProperties.ToString();

最后调试App,就会像下图一样了。

这里写图片描述

但是如你所见,文件名太长了却无法自动换行,而且数据也显示不完整。改改XAML中的TextBlock即可。TextWrapping属性设置为Wrap,则可以换行;将TextBlock添加到ScrollViewer内则会显示出一个滚动条。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button Width="200" Height="70" Name="btnGetProp" Content="获取文件属性" Click="btnGetProp_Click"/>
            <ScrollViewer>
                <TextBlock Name="tBlockProp" Margin="12" Width="480" FontSize="30" TextWrapping="Wrap"/>
            </ScrollViewer>
        </StackPanel>
    </Grid>

这里写图片描述

那么这一节就结束咯。下一篇再见!共同努力。



感谢您的访问,希望对您有所帮助。

欢迎大家关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


目录
相关文章
|
14天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
30天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
84 3
|
1月前
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
1月前
|
开发工具 数据安全/隐私保护 Android开发
【教程】APP 开发后如何上架?
【教程】APP 开发后如何上架?
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
API
uni-app 146朋友圈列表api开发
uni-app 146朋友圈列表api开发
18 0
|
1月前
|
JavaScript Android开发
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
【问题篇】打包Vue-cli3创建的vue项目成App的apk文件
25 0
|
30天前
|
开发框架 数据安全/隐私保护 开发者
HBuilder开发者必备!Windows上传IPA文件的软件分享
HBuilder开发者必备!Windows上传IPA文件的软件分享
21 1
|
3天前
|
存储 安全 文件存储
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
Windows系统本地部署HFS并结合内网穿透实现公网访问本地存储文件
|
11天前
|
Windows
【Windows】 手写脚本更快编辑hosts文件
【Windows】 手写脚本更快编辑hosts文件
11 0