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?
3 Answers
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 StorageFolder
s 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.

- 11,824
- 3
- 18
- 51
-
I really liked ur answer , just please can you clarify how can I store that parent folder which user will give me ? can that whole folder be stored in futureaccesslist? and then later each file in that folder can be accessed through code? – Muhammad Touseef Sep 28 '15 at 07:52
-
Yes, you just store it the same way you'd store a file. – Peter Torr - MSFT May 22 '16 at 18:39
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();
}
}
}

- 598
- 4
- 9
-
that is really good answer and very simple also, it is clear that I am storing each file in a list of class Appfiles using byte array system that is clear to me. but my question further is when i launch the app again how can I access those files again? are u suggesting that i may store that list data in a sqlite database? and then later use that data to access and read thos files again? is that possible? if yes then please can u tell me which method to use to read files from byte arrays? or do i need to convert them back to storage files? :) – Muhammad Touseef Sep 28 '15 at 07:55
-
SQLite is not necessary, please see the updated answer ;)! – Moisés Alexander Salazar Vila Sep 28 '15 at 20:19
-
the string key is the value which i can provide right? so that i can use it later to retrieve the file? i mean it can be any string associated with that file? and "T" value is the storage file which i want to save for later use right? – Muhammad Touseef Oct 25 '15 at 05:48
-
and also "Jsonconvert" is not being recognized, why is this so? and there are no namespaces to include in order to remove the error i tried including using Newtonsoft.Json; but this namespace is also not being recognized please help regarding this matter :) – Muhammad Touseef Oct 25 '15 at 05:53
-
you need add Json.NET with Nugget to your project and then just don't forget to add the namespace to your class: "using Newtonsoft.Json;" – Moisés Alexander Salazar Vila Oct 26 '15 at 07:08
-
yes i did that and i am able to acces it, it works fine when i save a List
to local settings and also when i load it back, the problem is that when i store IReadOnlyList – Muhammad Touseef Oct 26 '15 at 12:24it gets stored and i guess it gets stored as string instead of StorageFile. so when i try to retrieve it as IReadOnlyList it gives an exception saying this class doesnt have any constructor to return storage files :( please help -
i tried to first convert the file into byte array like u mentioned and then stored it into local settings but this exception came up WinRT information: Error trying to write application data container value Additional information: The size of the state manager setting value has exceeded the limit. – Muhammad Touseef Oct 26 '15 at 13:15
-
try using IReadOnlyList
.ToList() don't forget add System.Linq namespace; – Moisés Alexander Salazar Vila Oct 27 '15 at 13:44 -
or in the case of the byte array, use Convert.ToBase64String() https://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.110).aspx and split it :v! – Moisés Alexander Salazar Vila Oct 27 '15 at 13:48
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

- 97
- 7