[Wanli journey-Windows App development] file & amp; data-Get file attributes, Wanli journey app development

Source: Internet
Author: User

[Wanli journey-Windows App development] file & Data-Get file attributes, Wanli journey app development

This section shows how to get the file attributes, including the file name, type, and recent access time.

Create Button and TextBlock

The following code is simple.

<Grid Background = "{ThemeResource region}"> <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" verticalignment = "Center"> <Button Width = "200" Height = "70 "Name =" btnGetProp "Content =" Get file attributes "Click =" btnGetProp_Click "/> <TextBlock Name =" tBlockProp "Margin =" 12 "Width =" 480 "FontSize =" 30 "/> </StackPanel> </Grid> </Page>

In the Click event, you first get the image library. Of course, you can get other places. In the library files on my computer, only the document library and Image Library have files. Create a file query and assign all these files to files. The var here is very powerful. It is perfect to instantiate a StringBuilder object to help output information. We often use it before.

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();

In this way, the attributes of Name, FileType, Size, and DateModified are obtained. However, it is difficult to obtain other attributes, which are "extended attributes ".

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);

Finally, pass fileProperties to TextBlock.

tBlockProp.Text = fileProperties.ToString();

Finally, the App will be debugged like this.

But as you can see, the file name is too long but cannot be automatically wrapped, and the data is incomplete. Modify the TextBlock in XAML. If the TextWrapping attribute is set to Wrap, you can Wrap it. If you add TextBlock to ScrollViewer, a scroll bar is displayed.

<Grid Background = "{ThemeResource region}"> <StackPanel Orientation = "Horizontal" HorizontalAlignment = "Center" verticalignment = "Center"> <Button Width = "200" Height = "70 "Name =" btnGetProp "Content =" Get file attributes "Click =" btnGetProp_Click "/> <ScrollViewer> <TextBlock Name =" tBlockProp "Margin =" 12 "Width =" 480" fontSize = "30" TextWrapping = "Wrap"/> </ScrollViewer> </StackPanel> </Grid>

This section ends. Next Goodbye! Work together.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.