Search code examples
windows-10uwpfile-accessfilepicker

UWP apps accessing files from random location on system


in UWP there are files and permissions restrictions, so we can only acces files directly from few folders or we can use filepicker to access from anywhere on system. how can I use the files picked from filepicker and use them anytime again when the app runs ? tried to use them again by path but it gives permission error. I know about the "futureacceslist" but its limit is 1000 and also it will make the app slow if I am not wrong? . Is there a better way to do this ? or can we store storage files link somehow in local sqlite database?


Solution

  • 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();
            }
        }
    }