UWP – Win10 – Files and Folders P1

22 בJanuary 2017

אין תגובות
Introduction
For .Net developers who begin working on UWP apps it is must to upgrade the
knowledge base.

Well that API has changed now, and its happened since WIN10 development vision to unified the entire dev API for all platforms and devices under common types, meaning we no more writing code for devices but for OS.

Every application got an isolation built in storage to serve the application lifecycle.

In edition UWP application has no permissions to read a file from anywhere outside the application folder (will discussed later on) as hard drive. If you want to read a file in the user’s Documents Library you should either let the user select the file using a picker or drag file into out app surface (will discussed later on) or declare the folder in the application’s manifest – only for well defined places (like documents folder)

Universal Windows Introducing the StorageFolder and StorageFile types and the new API of LocalStorage:

For example:

private async void foo()

        {

            StorageFolder local = ApplicationData.Current.LocalFolder;

            var text = "hello world";

            var textFile =await local.CreateFileAsync("text.txt");

            await FileIO.WriteTextAsync(textFile, text);

        }

LocalStorage terms is a unique place who generated per application and allocated at C:\Users\UserName\AppData\Local\Packages\AppName

And include several options like cache temp settings etc.

Important Note- when removing the application from the computer the entire App-data folder will removed either, so even using it for storing data or not, must thinking about backup files

Privacy:

As mentioned earlier you need user permission to manipulate over files, and that can happened either 4 ways:

1-AppManifest by marking special folders as allowed, under capabilities section

 image.png

Btw, this option can be easily continuing by KnownFolders option of the API like:

StorageFolder folder = KnownFolders.MusicLibrary;

StorageFile file = await folder.CreateFileAsync("Test.doc", CreationCollisionOption.GenerateUniqueName);

2- by opening FilePicker

private async void pickerBtn_Click(object sender, RoutedEventArgs e)

        {

            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;

            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

            openPicker.FileTypeFilter.Add(".jpg");

            openPicker.FileTypeFilter.Add(".jpeg");

            openPicker.FileTypeFilter.Add(".png");


            StorageFile file = await openPicker.PickSingleFileAsync();

        }

3- by dragging files over the application surface (similar to the former) and will demonstrate on next post.

4 – by hacking, out of the scope here and don’t forget that Win10 application should populate via Microsoft Store. Meaning validation will performed.

The main difference between first way and 2-3 is the privacy assumption, the first can performed under the hood, so by default access is blocked,

and 2-3 is marshaling by the user so access can be easier.

Another folder who available for quick access is the downloads folder.

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *