During application development, you often need to open and save files. In Windows 8, the windows. Storage. pickers namespace provides a class for opening and saving files:
Class |
Description |
Fileextensionvector |
Indicates the random access set of the file extension. |
Fileopenpicker |
Specifies the selector that allows you to select and open the UI element of a file. |
Filepickerfiletypesorderedmap |
Indicates a set of display names mapped to the associated file type (Extension. |
Filepickerselectedfilesarray |
Indicates the set of stored files selected by the user using the file selector. |
Filesavepicker |
Indicates the selector that allows users to select file names, extensions, and storage locations for files. |
Folderpicker |
Specifies the selector that allows you to select the UI element of a folder. |
To use the File selector and file saver, you must add the selector declaration to the application. Double-click package. appxmanifest, select the "Declaration" tab, and add the file selector and file saver.
I.Fileopenpicker (File selector)
Attribute |
Description |
Commitbuttontext |
Obtains or sets the label text of the submit button of the file open selector. |
Filetypefilter |
Obtains the set of document types displayed by the file open selector. |
Settingsidentifier |
Gets or sets the Set Identifier associated with the state of the file open selector. |
Suggestedstartlocation |
Obtain or set the initial position of the file to be presented to the user by the file open selector. |
Viewmode |
Obtains or sets the file opening selector to display the view mode of the project. |
Method |
Description |
Pickmultiplefilesasync |
Displays the file selector so that you can select multiple files. |
Picksinglefileasync |
The file selector is displayed so that you can select a file. |
1 /// <summary> 2 /// select a single file 3 /// </Summary> 4 /// <Param name = "sender"> </param> 5/ // <Param name = "E"> </param> 6 async private void btnopenfile_click (Object sender, routedeventargs e) 7 {8 fileopenpicker filepicker = new fileopenpicker (); 9 filepicker. commitbuttontext = "OK"; 10 filepicker. filetypefilter. add (". TXT "); 11 filepicker. suggestedstartlocation = pickerlocationid. documentslibrary; 12 filep Icker. viewmode = pickerviewmode. thumbnail; 13 storagefile = await filepicker. picksinglefileasync (); 14 if (storagefile! = NULL) 15 {16 string filecontent = await fileio. readtextasync (storagefile); 17 tbkcontent. TEXT = filecontent; 18} 19} 20 21 /// <summary> 22 /// select multiple files 23 /// </Summary> 24 /// <Param name = "sender"> </param> 25 // <Param name = "E"> </param> 26 async private void btnmultiopenfile_click (Object sender, routedeventargs e) 27 {28 fileopenpicker filepicker = new fileopenpicker (); 29 filepicker. commitbuttontext = "true Set "; 30 filepicker. filetypefilter. add (". TXT "); 31 filepicker. suggestedstartlocation = pickerlocationid. documentslibrary; 32 filepicker. viewmode = pickerviewmode. thumbnail; 33 ireadonlylist <storagefile> storagefilelist = await filepicker. pickmultiplefilesasync (); 34 if (storagefilelist! = NULL) 35 {36 foreach (storagefile file in storagefilelist) 37 {38 // todo: Continue File Operations 39} 40 41} 42}
II.Filesavepicker)
Attribute |
Description |
Commitbuttontext |
Obtain or set the label text of the submit button in the pickerui file. |
Defaultfileextension |
Gets or sets the default file extension of the file saved through filesavepicker. |
Filetypechoices |
Obtains a set of valid file types that can be allocated to a file. |
Settingsidentifier |
Gets or sets the identifier associated with the current filesavepicker instance. |
Suggestedfilename |
Obtains or sets the recommended file name from the file storage selector. |
Suggestedsavefile |
Obtains or sets the storagefile recommended by the file selector for saving files. |
Suggestedstartlocation |
Obtains or sets the recommended location from the file storage selector as the location of the file to be saved. |
Method |
Description |
Picksavefileasync |
Displays the file selector so that you can save the file and set the file name, extension, and location of the file to be saved. |
1 /// <summary> 2 /// file storage device 3 /// </Summary> 4 /// <Param name = "sender"> </param> 5/ // <Param name = "E"> </param> 6 async private void btnsavefile_click (Object sender, routedeventargs e) 7 {8 filesavepicker filepicker = new filesavepicker (); 9 filepicker. commitbuttontext = "save"; 10 filepicker. suggestedstartlocation = pickerlocationid. documentslibrary; 11 filepicker. defaultfileextension = ". TXT "; 12 filepicker. filetypechoices. add ("TXT", new list <string> (){". TXT "}); 13 filepicker. suggestedfilename = datetime. now. tostring ("yyyymmddhhmmss "). tostring (); 14 storagefile = await filepicker. picksavefileasync (); 15 await fileio. writetextasync (storagefile, "Test Data", unicodeencoding. utf8); 16}
3. folderpicker)
Attribute |
Description |
Commitbuttontext |
Obtains or sets the label text of the submit button of the folder selector. |
Filetypefilter |
Obtains the set of document types displayed by the folder selector. |
Settingsidentifier |
Gets or sets the identifier associated with the current folderpicker instance. |
Suggestedstartlocation |
Obtain or set the folder selector to find the initial location of the folder to be presented to the user. |
Viewmode |
Gets or sets the folder selector to display the view mode of the project. |
Method |
Description |
Picksinglefolderasync |
Show the folderpicker object so that you can select a folder. |
1 /// <summary> 2 /// folder selector 3 /// </Summary> 4 /// <Param name = "sender"> </param> 5 // /<Param name = "E"> </param> 6 async private void btnselectfolder_click (Object sender, routedeventargs e) 7 {8 folderpicker = new folderpicker (); 9 folderpicker. commitbuttontext = "select"; 10 folderpicker. filetypefilter. add (". TXT "); 11 folderpicker. filetypefilter. add (". jpg "); 12 folderpicker. filetypefilter. add (". PNG "); 13 folderpicker. filetypefilter. add (". doc "); 14 folderpicker. suggestedstartlocation = pickerlocationid. documentslibrary; 15 folderpicker. viewmode = pickerviewmode. thumbnail; 16 storagefolder = await folderpicker. picksinglefolderasync (); 17 // Add the folder 18 storagefolder newfolder = await storagefolder Under the selected folder. createfolderasync ("new folder", creationcollisionoption. generateuniquename); 19}
Source code download