[Wanli journey-Windows App development] file & amp; data-Read File/folder name, Wanli journey app development

Source: Internet
Author: User

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

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.