file system access
DESCRIPTION
File System Access. Access to known folders, using pickers, writing to and reading from files, caching files for future access. George Georgiev. Telerik Software Academy. academy.telerik.com. Technical Trainer. itgeorge.net. Table of Contents. Store Apps File Access - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/1.jpg)
File System AccessAccess to known folders, using pickers, writing to and
reading from files, caching files for future access
George Georgiev
Telerik Software Academyacademy.telerik.com
Technical Traineritgeorge.net
![Page 2: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/2.jpg)
2
Table of Contents Store Apps File Access
Capabilities, Prompting for Access Accessing "Known Folders"
Documents, pictures, etc. Pickers Folder Picker File Open Picker Reading and writing to a file File Save Picker Storing Files in the Access Cache
![Page 3: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/3.jpg)
Store Apps File AccessRestrictions, Capabilities, Prompting for Access
![Page 4: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/4.jpg)
4
Store Apps File Access Store Apps don't have typical access to files Only to the per-app virtual file
system Several ways of accessing the file system Through known folders
Documents, Pictures, Removable Storage, etc.
Need to add capabilities in app manifest
Through file/folder pickers System-controlled dialogs/prompts to
the user Through other apps
Participating in file picker or share contracts
![Page 5: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/5.jpg)
Store Apps File Access WinRT Storage APIs
Provide file pickers & file picker contracts
Provide KnownFolders enumeration Provide file input/output
operations, file/folder creation, renaming, deleting, etc.
Other storage locations and options Namespace – Windows.Storage
5
![Page 6: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/6.jpg)
Accessing Known FoldersAdding capabilities, using the storage
API
![Page 7: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/7.jpg)
7
Accessing Known Folders
Known folders are typical folders in Windows Documents library, Music library,
etc. Apps can access them directly
If they define this capability in the app manifest
Adding known folders capabilities Go to the manifest, under
Capabilities Pick the folder(s) needed for the app
Under declarations, add File Type Associations Customize for files your app will
access in the known folders
![Page 8: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/8.jpg)
Accessing Known Folders
After editing the manifest, use the storage API E.g.
Windows.Storage.KnownFolders.documentsLibrary
The API returns a StorageFolder object
Once you have a StorageFolder, you can: Access file types declared in the
manifest Read, write, rename, delete, etc.
Create any file type Open & create subfolders
8
![Page 9: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/9.jpg)
Accessing Known Folders
Live Demo
![Page 10: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/10.jpg)
PickersWinows.Storage.Pickers
![Page 11: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/11.jpg)
11
Pickers Pickers are special views in Store Apps Controlled and styled by the system Limited customization options for
apps Button text, file display mode, visible
files (by extension), suggestedStartLocation etc.
Asynchronously pick files/folders Visualize picking UI for the user App keeps running as if on-screen On successful pick, app gets access
to picked item App can store the file for future
access Windows.Storage.Pickers
![Page 12: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/12.jpg)
Folder PickerIntroduction to pickers, picking a
folder
![Page 13: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/13.jpg)
13
Folder Picker Folder pickers provide UI for picking folders Result is received asynchronously
pickSingleFolderAsync() returns a promise
Result value is a StorageFolder like when using Known Folders
Requires a FileTypeFilter to be set Files listed to the user when
navigating List/array of strings (extensions, e.g.
".txt", "*") Doesn't require any capability
declarations Except if using Known Folders for
suggestedStartLocation
![Page 14: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/14.jpg)
Folder PickerLive Demo
![Page 15: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/15.jpg)
File Open PickerOpening existing files
![Page 16: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/16.jpg)
16
File Open Picker File Open pickers open existing files Result is received asynchronously
pickSingleFileAsync()/pickMultipleFilesAsync() return a promise
Result value is a StorageFile Read, write, rename, delete, copy,
move, properties, thumbnail, etc. Requires FileTypeFilter (same as in
FolderPicker) Doesn't require any capability
declarations Except if using Known Folders for
suggestedStartLocation
![Page 17: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/17.jpg)
File Open PickerLive Demo
![Page 18: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/18.jpg)
Reading and Writing Files
Using Winows.Storage.FileIO
![Page 19: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/19.jpg)
19
Reading an Writing files Access to file contents happens through Windows.Storage.FileIO Provides several read and write
methods Reading, writing & appending
strings, bytes, etc. Operations are asynchronous
(return promises) Read operation get content in
success handler Write operations could skip handling
Except for success/error notification
Windows.Storage.FileIO.writeTextAsync(file, text) .done(writeSuccessMessage, writeErrorMessage);Windows.Storage.FileIO.readTextAsync(file) .done(function (text) {/*use text here*/})
![Page 20: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/20.jpg)
Reading and Writing Files
Live Demo
![Page 21: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/21.jpg)
Transacted File WriteUsing transaction stream to ensure
write integrity
![Page 22: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/22.jpg)
22
Transacted File Write Some apps need stream operations over files Streams are supported by FileIO Stream writing can be interrupted
by an error Losing part of the data, corrupting
files For writes that need to be atomic
use transactStream from WinRT Works asynchronously Ensures a full write or no write at all
![Page 23: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/23.jpg)
Transacted File WriteLive Demo
![Page 24: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/24.jpg)
File Save PickerCreating files from Apps
![Page 25: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/25.jpg)
25
File Save Picker File Save pickers create or overwrite files Created file is received
asynchronously pickSaveFileAsync() returns a
promise Result value is a StorageFile Requires FileTypeChoices (key-value
pairs) Key: the user-friendly file type (e.g.
"Web Page") Value: list of strings, possible
filename extensions (e.g. [".htm", ".html"])
Doesn't require any capability declarations Except if using Known Folders for
suggestedStartLocation
picker.fileTypeChoices.insert("Plain Text", [".txt"])
![Page 26: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/26.jpg)
File Save PickerLive Demo
![Page 27: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/27.jpg)
Storing Files in the Access CacheCreating files from Apps
![Page 28: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/28.jpg)
28
Storing Files in Access Cache
Apps access files through StorageFile objects Folders through StorageFolder
objects Apps can't create such objects on
their own E.g. need a file picker to do it Can't save the URI of a file and
access it Unless file is in AppData
WinRT supports an API which saves StorageFiles and persists them Called futureAccessList Key-value pairs (string token –
StorageFile pairs)
![Page 29: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/29.jpg)
Storing Files in Access Cache
Using futureAccessList
Insert the StorageFile the app will need later
The file needs a unique string token (e.g. filename + timestamp)
The file can be accessed at any time (even after app has restarted)
29
var accessCache = Windows.Storage.AccessCache;var futureAccessList = accessCache. StorageApplicationPermissions.futureAccessList;
futureAccessList.addOrReplace(token, file);
futureAccessList.getFileAsync(token) .then(function(file){/*use file here*/});
![Page 30: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/30.jpg)
Storing Files in Access Cache
Live Demo
![Page 31: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/31.jpg)
форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно
програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки
уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop
уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC
безплатен курс "Разработка на софтуер в cloud среда"
BG Coder - онлайн състезателна система - online judge
курсове и уроци по програмиране, книги – безплатно от Наков
безплатен курс "Качествен програмен код"
алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET
курсове и уроци по програмиране – Телерик академия
курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране
Николай Костов - блог за програмиранеC# курс, програмиране, безплатно
?? ? ?
??? ?
?
? ?
??
?
?
? ?
Questions?
?
File System Access
http://academy.telerik.com
![Page 32: File System Access](https://reader033.vdocuments.mx/reader033/viewer/2022051700/568162a3550346895dd31f7e/html5/thumbnails/32.jpg)
Exercises1. Implement a simple Video Player app
The app should be able to open video files through file pickers
The opened files should be stored into an in-app playlist
The app should be able to play any file from the playlist
* after a file has finished playing, the next one should start
The app should be able to remove songs from the playlist
The app should be able to save the playlist through a file save picker
Save the file in any format you like (e.g. your own or a one of the built into Windows)
The app should be able to load a any playlist it saved through a file open picker and be able to play its files
* buttons for adding/removing songs should be in AppBar