[Wanli journey-Windows App development] file & Data-Read File/folder name, Wanli journey app development
In the previous section, we learned how to bind data, because I am personally satisfied with the previous article, at least relative to the previous one (I also know that the previous one is too bad, it will be modified later, and the blog will be like the software ). At the beginning of this section, we will see how Windows apps operate files.
Read the file name and folder name on Windows
First, we define a Button and TextBlock in XAML to write the process of reading the file/folder name in the click Event of the former, and the latter to display the file information.
<Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation = "Horizontal"> <Button Name = "btnGetName" Width = "200" Height = "100" Content = "read file Name "Click =" btnGetName_Click "/> <TextBlock Name =" textBlockFileName "Width =" 500 "Height =" 300 "FontSize =" 30 "Margin =" 12 "/> </ stackPanel> </Grid>
The following code first reads the image library through the StorageFolder class, and then asynchronously loads the file and Folder Information of the image library into the corresponding List. Create a StringBuilder to save the information of these files. Here, only the Name attribute of the file/folder is used, but there are many attributes, such as the Path attribute. Finally, assign the obtained information to TextBlock.
private async void btnGetName_Click(object sender, RoutedEventArgs e) { StorageFolder pictureFolder = KnownFolders.PicturesLibrary; IReadOnlyList<StorageFile> pictureFileList = await pictureFolder.GetFilesAsync(); IReadOnlyList<StorageFolder> pictureFolderList = await pictureFolder.GetFoldersAsync(); StringBuilder picutreFolderInfo = new StringBuilder(); foreach(StorageFile f in pictureFileList) { picutreFolderInfo.Append(f.Name+"\n"); } foreach(StorageFolder f in pictureFolderList) { picutreFolderInfo.Append(f.Name+"\n"); } textBlockFileName.Text = picutreFolderInfo.ToString(); }
Add async to the method name. We also need to declare in the list file that our application will use the image library, which will be the same in Windows Phone later.
Read the file name and folder name on Windows Phone
The background Code does not need to be modified. You only need to modify the XAML code to adapt to the screen ~
<Grid> <StackPanel Orientation = "Vertical"> <Button Name = "btnGetName" Width = "150" Height = "70" HorizontalAlignment = "Center" Content = "Read File Name" Click = "btnGetName_Click"/> <TextBlock Name = "textBlockFileName" Width = "300" Height = "300" FontSize = "30" Margin = "12" TextWrapping = "Wrap"/> </StackPanel> </Grid>
Other methods for reading file names
private async void btnGetName_Click(object sender, RoutedEventArgs e) { StorageFolder picutureFolder = KnownFolders.PicturesLibrary; StringBuilder pictureFolderInfo = new StringBuilder(); IReadOnlyList<IStorageItem> pictureFileItem = await picutureFolder.GetItemsAsync(); foreach(var i in pictureFileItem) { if (i is StorageFolder) pictureFolderInfo.Append(i.Name + "\n"); else pictureFolderInfo.Append(i.Name + "\n"); } textBlockFileName.Text = pictureFolderInfo.ToString(); }