UWP apps accessing files from random location on system

ε祈祈猫儿з 提交于 2019-12-04 09:37:17

Considering this method..

    public async static Task<byte[]> ToByteArray(this StorageFile file)
    {
        byte[] fileBytes = null;
        using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
        {
            fileBytes = new byte[stream.Size];

            using (DataReader reader = new DataReader(stream))
            {
                await reader.LoadAsync((uint)stream.Size);
                reader.ReadBytes(fileBytes);
            }
        }

        return fileBytes;
    }

This class..

    public class AppFile
    {
        public string FileName { get; set; }
        public byte[] ByteArray { get; set; }
    }

And this variable

    List<AppFile> _appFiles = new List<AppFile>();

Just..

    var fileOpenPicker = new FileOpenPicker();
    IReadOnlyList<StorageFile> files = await fileOpenPicker.PickMultipleFilesAsync();

    foreach (var file in files)
    {
        var byteArray = await file.ToByteArray();
        _appFiles.Add(new AppFile { FileName = file.DisplayName, ByteArray = byteArray });
    }

UPDATE

using Newtonsoft.Json;
using System.Linq;
using Windows.Security.Credentials;
using Windows.Storage;

namespace Your.Namespace
{
    public class StateService
    {
        public void SaveState<T>(string key, T value)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            localSettings.Values[key] = JsonConvert.SerializeObject(value);
        }

        public T LoadState<T>(string key)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Values.ContainsKey(key))
                return JsonConvert.DeserializeObject<T>(((string)    localSettings.Values[key]));
            return default(T);
        }

        public void RemoveState(string key)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Values.ContainsKey(key))
                localSettings.Values.Remove((key));
        }

        public void Clear()
        {
            ApplicationData.Current.LocalSettings.Values.Clear();
        }
    }
}

If you need to access lots of files, asking the user to select the parent folder and then storing that is probably a better solution (unless you want to store 1,000 individually-picked files from different locations). You can store StorageFolders in the access list as well.

I'm not sure why you think it will make your app slow, but the only real way to know if this will affect your performance is to try it and measure against your goals.

A bit late, but, yes the future access list will slow down your app in that it returns storagfile, storagefolder, or storeageitem objects. These run via the runtime broker which hits a huge performance barrier at about 400 objects regardless of the host capability

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!