Windows 10 Development Basics-Files, folders, and libraries (ii)

Source: Internet
Author: User

Main content:

To Open and save a file using the picker

About files, folders, and libraries, if you dig into the fact that there are more content, we will learn the picker this time. There are three ways to read and write text files in the last blog post.

The file picker includes the File Open picker (Fileopenpicker, Folderpicker), and the File Save Picker (Filesavepicker), which are used to open the file and save the file, respectively, using the same methods as the two pickers.

Fileopenpicker class:

    • ViewMode , Gets or sets the view mode used by the file picker to present a file or folder, the property value is specified by the Pickerviewmode enumeration, which has two enumeration values, the list represents a listing pattern, and thumbnail represents the thumbnail mode

    • suggestedstartlocation, Gets or sets the initial location of the file to be rendered to the user by the file picker. The property value is specified by the Pickerlocationid enumeration, which has 10 enumerated values and goes to the definition itself to view.

    • Filetypefilter, gets a collection of file types displayed by the file selector, which can be ". txt", ". jpg", and so on, using Add.

Note that we set the ViewMode and Suggestedstartlocation values after the first run of the application, and then switch other values to run, sometimes it does not work, it should be the operating system has its own records, and then this filetypefilter property must be specified.

Fileopenpicker picker = new Fileopenpicker (); Create File Open Picker
Picker.       ViewMode = Pickerviewmode.thumbnail; Set the value of ViewMode to thumbnail
Picker.  Suggestedstartlocation =pickerlocationid.pictureslibrary; Set the start position of the file picker open file to the picture library

Picker. Filetypefilter.add (". jpg");
Picker. Filetypefilter.add (". jpeg");
Picker. Filetypefilter.add (". png");

Folderpicker class:

the properties and usages of this class are similar to Fileopenpicker.

Folderpicker folderpicker = new Folderpicker ();
Folderpicker.viewmode = pickerviewmode.list;
Folderpicker.suggestedstartlocation = pickerlocationid.pictureslibrary;

Filesavepicker class:

The 3 attributes that we often use in this class are Suggestedstartlocation (IBID.), Filetypechoices, Suggestedfilename. Filetypechoices is a dictionary type (idictionary<system.string, ilist<system.string>>) that gets a collection of valid file types that the user can choose to assign to a file. Suggestedfilename is to get or set the file name that the file save picker suggests to the user.

Filesavepicker picker = new Filesavepicker ();

Picker. Suggestedstartlocation = pickerlocationid.pictureslibrary;

Picker. Filetypechoices.add ("Image", new list<string> () {". jpg", ". jpeg", ". png", ". bmp", ". gif"});

Picker. Suggestedfilename ("Yyyymmddhhmmss");

Let's start with a regular demo, with a single file, multiple files, and folders.

Select and display a picture:

   Private Async voidBtn_pickpic_click (Objectsender, RoutedEventArgs e) {Fileopenpicker Picker=NewFileopenpicker (); Picker. ViewMode=pickerviewmode.list; Picker. Suggestedstartlocation=pickerlocationid.pictureslibrary; Picker. Filetypefilter.add (". jpg"); Picker. Filetypefilter.add (". JPEG"); Picker. Filetypefilter.add (". PNG"); Storagefile=awaitPicker.            Picksinglefileasync (); if(Storagefile! =NULL)            {                 This. Tb_pickedpic. Text ="Picked Photo:"+Storagefile.                Name; WriteableBitmap WriteableBitmap=NewWriteableBitmap ( -, -); Irandomaccessstream Stream=awaitStoragefile?.                OpenAsync (; awaitWriteablebitmap.setsourceasync (stream); Image. Source=WriteableBitmap; }            Else            {                 This. Tb_pickedpic. Text ="operation cancelled."; Image. Source=NULL; }        }

Select more than one picture:

  Private Async voidBtn_pickmultipic_click (Objectsender, RoutedEventArgs e) {            varPicker =NewFileopenpicker (); Picker. ViewMode=Pickerviewmode.thumbnail; Picker. Suggestedstartlocation=pickerlocationid.pictureslibrary; Picker. Filetypefilter.add (". JPEG"); Picker. Filetypefilter.add (". PNG"); Picker. Filetypefilter.add (". jpg"); Ireadonlylist<StorageFile> filelist =awaitPicker.            Pickmultiplefilesasync (); StringBuilder Output=NewStringBuilder ("Picked Files:"); if(FileList. Count>0)            {                foreach(varFileinchfilelist) {output. Append (file. Name+"\ n"); }            }            Else{output. Append ("none!"); } showmsg (output.        ToString ()); }

ShowMsg (output. ToString ()); Is a popup dialog that displays the selected picture.

Select a folder:

   Private Async voidBtn_pickfolder_click (Objectsender, RoutedEventArgs e) {Folderpicker Folderpicker=NewFolderpicker (); Folderpicker.viewmode=pickerviewmode.list; Folderpicker.suggestedstartlocation=pickerlocationid.pictureslibrary; FOLDERPICKER.FILETYPEFILTER.ADD ("*"); Storagefolder folder=awaitFolderpicker.picksinglefolderasync (); if(Folder! =NULL) {StorageApplicationPermissions.FutureAccessList.AddOrReplace ("Pickedfoldertoken", folder); ShowMsg ("Picked folder:"+folder.            Name); }            Else{showmsg ("operation cancelled."); }        }

Then save the file, first save the text file, and then save the image you selected and display to a different folder.

Save the text file:

  Private Async voidButton_Click (Objectsender, RoutedEventArgs e) {Filesavepicker Savepicker=NewFilesavepicker (); Savepicker.suggestedstartlocation=pickerlocationid.documentslibrary; SAVEPICKER.FILETYPECHOICES.ADD ("text File",Newlist<string> () {". txt" }); Savepicker.suggestedfilename="Mytxt"; StorageFile file=awaitSavepicker.picksavefileasync (); if(File! =NULL) {cachedfilemanager.deferupdates (file); if(Tbxcontent. Text.trim (). Length >0)                {                    awaitFileio.writetextasync (file,tbxcontent.                    Text); Fileupdatestatus Status=awaitcachedfilemanager.completeupdatesasync (file); if(Status = =fileupdatestatus.complete) { This. Tbinfo.text ="File"+ file. Name +"was saved."; }                    Else                    {                         This. Tbinfo.text ="File"+ file. Name +"couldn ' t be saved."; }                }            }            Else            {                 This. Tbinfo.text ="operation cancelled."; }        }

Operation Result:

Save the picture that you selected above and show to another folder:      

   Private Async voidBtn_save_click (Objectsender, RoutedEventArgs e) {Filesavepicker Picker=NewFilesavepicker (); Picker. Suggestedstartlocation=pickerlocationid.pictureslibrary; Picker. Filetypechoices.add ("Image",Newlist<string> () {". jpg",". JPEG",". PNG",". bmp",". gif" }); Picker. ("YYYYMMDDHHMMSS"); StorageFile file=awaitPicker.            Picksavefileasync (); if(file!=NULL&&storagefile!=NULL) {cachedfilemanager.deferupdates (file); IBuffer Buffer=awaitFileio.readbufferasync (Storagefile);//Storagefile is already selected to save the file                awaitfileio.writebufferasync (file, buffer); Fileupdatestatus Status=awaitcachedfilemanager.completeupdatesasync (file); }        }

Operation Result:

Well, these are some of the things about the file picker. See you next time!

Windows 10 Development Basics-Files, folders, and libraries (ii)

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