I have the following scenario in my UWP App:
The user imports a file (e.g. using a FilePicker
, or by dragging a file onto the app, etc.) into my application.
We save some metadata of this file inside our internal database, and we also save a token retrieved using the following code:
string token = StorageApplicationPermissions.FutureAccessList.Add(file);
The user closes the app.
The user re-opens the app.
Now we want to overwrite the initial file. To do this, we retrieve the StorageFile using this code:
StorageFile exportTarget = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
but when I try to write into the file, e.g. using
using (IRandomAccessStream fs = await exportTarget.OpenAsync(FileAccessMode.ReadWrite))
I get an exception complaining that exportTarget
is ReadOnly.
How can I have access to such file in Write mode?
It turned out it was not a strict problem of the StorageApplicationPermissions.FutureAccessList
.
The problem happens in fact only if the user imports a file by dragging-and-dropping the file into the app: in this case, the DataPackageView.GetStorageItemsAsync()
method returns a list of StorageFile
that are ReadOnly.
So, the only way to overwrite those files is by using PathIO
methods, as described in this post: https://github.com/microsoft/microsoft-ui-xaml/issues/2421