WinRT – file access and serialization

File access and secure mode of WinRT was my first surprise … In all the examples on the NET, only LocalStorage is used … and that is understandable because it is only in the application directory that can be of access files by code.

For others, it must go through a FilePicker which provides you the desired StorageFile – through a user action. Otherwise create a StorageFile from a path as a string causes an exception …

Then I have a bad surprise with XML Serialization … pufffftt! Nothing of what I had in NET does. So I made a few attempts to realize that XML support was far from original.

There is for example no way to control the attributes that you want to serialize or not. So I leave the track [Xml …] to the detriment of [DataContract] allowing more thing.

Following this, a little digging and I discovered that FutureAccessList in StorageApplicationPermissions can find tokens and access directories or files to which the user has previously given you rights!

You will find below a helper to manage files with methods that allow you to do all this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;

namespace CBR.WinRT.Core.Components
{
    sealed class FileHelper
    {
        static public async Task<bool> FileExistAsync(StorageFolder folder, string fileName)
        {
            try
            {
                await folder.GetFileAsync(fileName);
                return true;
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Diagnostics.Debug.WriteLine("File doesn't exist. Use default.");
                return false;
            }
        }

        static public bool VerifyFileAccess(string filePath)
        {
            return StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == filePath).Count() != 0 ? true : false;
        }

        static public void AddFileAccess(IStorageItem file)
        {
            if (file != null)
            {
                //add to file access
                if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == file.Path).Count() == 0)
                    StorageApplicationPermissions.FutureAccessList.Add(file, file.Path);
            }
        }

        static public async Task<StorageFile> GetFileAccess(string filePath)
        {
            if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == filePath).Count() != 0)
            {
                var token = StorageApplicationPermissions.FutureAccessList.Entries.Single(p => p.Metadata == filePath).Token;
                return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
            }
            else
                return null;
        }

        static public async Task<StorageFolder> GetFolderAccess(string folderPath)
        {
            if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == folderPath).Count() != 0)
            {
                var token = StorageApplicationPermissions.FutureAccessList.Entries.Single(p => p.Metadata == folderPath).Token;
                return await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(token);
            }
            else
                return null;
        }

        static public async Task<StorageFile> CBRPickSingleFile()
        {
            List<string> filters = new List<string>() { ".cbz", ".cbr", ".zip", ".rar" };

            return await PickSingleFile( PickerViewMode.List, filters );
        }

        static public async Task<StorageFile> PickSingleFile(PickerViewMode mode, IList<string> filters)
        {
            var picker = new FileOpenPicker();
            picker.ViewMode = mode;
            foreach (string filter in filters)
                picker.FileTypeFilter.Add(filter);

            var file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                //add to file access
                if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == file.Path).Count() == 0)
                    StorageApplicationPermissions.FutureAccessList.Add(file, file.Path);

                return file;
            }
            else return null;
        }

        static public async Task<StorageFile> CBRPickMultipleFiles()
        {
            List<string> filters = new List<string>() { ".cbz", ".cbr", ".zip", ".rar" };

            return await PickSingleFile(PickerViewMode.List, filters);
        }

        static public async Task<IReadOnlyList<StorageFile>> PickMultipleFiles(PickerViewMode mode, IList<string> filters)
        {
            var picker = new FileOpenPicker();
            picker.ViewMode = mode;
            foreach (string filter in filters)
                picker.FileTypeFilter.Add(filter);

            var files = await picker.PickMultipleFilesAsync();
            if (files != null)
            {
                //add to file access
                foreach (StorageFile item in files)
                {
                    if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == item.Path).Count() == 0)
                        StorageApplicationPermissions.FutureAccessList.Add(item, item.Path);
                }
                return files;
            }
            else return null;
        }

        static public async Task<StorageFolder> CBRPickSingleFolder()
        {
            List<string> filters = new List<string>() { ".cbz", ".cbr", ".zip", ".rar" };

            return await PickSingleFolder(PickerViewMode.List, filters);
        }

        static public async Task<StorageFolder> PickSingleFolder(PickerViewMode mode, IList<string> filters)
        {
            var picker = new FolderPicker();
            picker.ViewMode = mode;
            foreach (string filter in filters)
                picker.FileTypeFilter.Add(filter);

            var folder = await picker.PickSingleFolderAsync();
            if (folder != null)
            {
                //add to access
                if (StorageApplicationPermissions.FutureAccessList.Entries.Where(p => p.Metadata == folder.Path).Count() == 0)
                        StorageApplicationPermissions.FutureAccessList.Add(folder, folder.Path);
                return folder;
            }
            else return null;
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s