Reputation: 33
I'm working in a UWP app, at some point, the user has the chance to save an image (done) by now I only let the user know that the save operation was done by a "messageDialog", this is my code:
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("QR Code", new List<string>() { ".png", ".jpg", ".jpeg", ".jpe", ".bmp" });
savePicker.SuggestedFileName = "QR_01";
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
Windows.Storage.CachedFileManager.DeferUpdates(file);
await Windows.Storage.FileIO.WriteBytesAsync(file, _bytes3);
Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("File saved on: " + file.Path);
await messageDialog.ShowAsync();
}
else
{
//this.textBlock.Text = "File " + file.Name + " couldn't be saved."; not defined by now
}
done = true;
}
else
{
this.textBlock.Text = "operation canceled";
done = false;
}
This works good so far but I want to be sure that the file is already there after the "WriteBytesAsync" method. So, this is the thing, the users can save the image in any directory they have acces to so I can't check (or read) the folder they choose afer that because I don't have acces permission afer save the file. I have tryed something like this to get folder access:
string savePath = System.IO.Path.GetDirectoryName(file.Path);
Windows.Storage.StorageFolder saveFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(savePath);
In order to use the "GetFileAsync" to check the file or the "FutureAccessList" for a future access but since I have file access permisions to any directory it fails, and because of that I can't use any of the solution in here: UWP Check If File Exists
My Questions are:
1.- Is there a way to check if the file was saved or successfully written afer the user click the "Save" button?
2.- Does the "WriteBytesAsync" method is enough to ensure taht the file was saved?? according to the info FileIO.WriteBytesAsync(IStorageFile, Byte[]) Method, it don't throw any exception.
Upvotes: 0
Views: 619
Reputation: 32775
but I want to be sure that the file is already there after the "WriteBytesAsync" method.
For you requirement, you could use GetFileFromPathAsync
method to get the file, if the file is not null, means that the file is exist. And please note if you want to above method, you need add broadFileSystemAccess
capability, and open it in your system setting. For more please refer this.
Windows.Storage.CachedFileManager.DeferUpdates(file);
await Windows.Storage.FileIO.WriteBytesAsync(file, new byte[3]);
var temp = await StorageFile.GetFileFromPathAsync(file.Path);
if(temp != null)
{
}
else
{
}
Does the "WriteBytesAsync" method is enough to ensure taht the file was saved??
Yep, WriteBytesAsync
method need to pass StorageFile
parameter. if the file parameter is not null means it has created in the folder. Or it will throw null exception.
Upvotes: 1