[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.