29
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

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

Embed Size (px)

Citation preview

Page 1: 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

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

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

Store Apps File AccessRestrictions, Capabilities, Prompting for Access

Page 4: 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

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

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

Accessing Known FoldersAdding capabilities, using the storage

API

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

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

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

Accessing Known Folders

Live Demo

Page 10: 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

PickersWinows.Storage.Pickers

Page 11: 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

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

Folder PickerIntroduction to pickers, picking a

folder

Page 13: 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

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

Folder PickerLive Demo

Page 15: 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

File Open PickerOpening existing files

Page 16: 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

16

File Open Picker File Open pickers open existing files Result is received asynchronously

PickSingleFileAsync()/PickMultipleFilesAsync() return an awaitable IAsyncOperation

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

File Open PickerLive Demo

Page 18: 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

Reading and Writing Files

Using Winows.Storage.FileIO

Page 19: 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

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

(awaitable) Read operation get content in

success handler Write operations could skip handling

Except for success/error notification

await Windows.Storage.FileIO.WriteTextAsync(file, text); //here you can notify of success/error (wrap in try-catch)var text = await Windows.Storage.FileIO.ReadTextAsync(file);//use text here or notify of error (wrap in try-catch)

Page 20: 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

Reading and Writing Files

Live Demo

Page 21: 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

File Save PickerCreating files from Apps

Page 22: 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

22

File Save Picker File Save pickers create or overwrite files Created file is received

asynchronously PickSaveFileAsync() returns an

awaitable 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

var textFileTypes = new List<string>(new string[]{".txt"});picker.FileTypeChoices.Insert("Plain Text", textFileTypes);

Page 23: 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

File Save PickerLive Demo

Page 24: 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

Storing Files in the Access CacheCreating files from Apps

Page 25: 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

25

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

Storing Files in Access Cache

Using futureAccessList

Insert the StorageFile the app will need later

Can place a unique string token (e.g. filename + timestamp, if not, the system randomizes one)

The file can be accessed at any time (even after app has restarted) 26

var accessCache = Windows.Storage.AccessCache;var futureAccessList = accessCache. StorageApplicationPermissions.FutureAccessList;

futureAccessList.Add(file);

var file = await futureAccessList.GetFileAsync(token);//use file here

Page 27: 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

Storing Files in Access Cache

Live Demo

Page 28: 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

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен 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, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

File System Access

http://academy.telerik.com

Page 29: 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

Exercises1. Take the PaintRT app you wrote for the

Gestures and Responding to Interactions homework and implement a Save file feature and an Open File feature Save the file in your own format, so that all

generated objects are loadable Open files your app created, enabling the user

to continue working on them, as if the app wasn't closed – e.g. users should still be able to change the color of an object by double-tapping it

* Bonus (not obligatory): add a feature for exporting drawings as image files (e.g. Bitmap, PNG, etc.)